1

I am writing html and javascript code to get some data from a Json file. My html code:

 <div class="section">
    <div class="sectionTitle">
      <h3>Configuration</h3>
    </div>
     <select name="selectDistribution" class="span12" onchange="callDist("value");"  

 onfocus="this.selectedIndex = -1;">
      <option >Choose from distributions.</option>
      <option value="1">Uniform</option>
      <option value="2">Normal</option>
      <option value="3">Exponential</option>
      <option value="4">Geometric</option>
</select>
    <div id="parameters"></div>
    <div id="distributionParams"> </div>
    <button class="btn btn-large btn-block btn-primary" type="button">Send</button>
  </div>

When a user chooses one distribution the parameters must be shown below the selection. For example when the user selects Uniform distribution min and max values must be attached below and I want to show first min value label and its value in text box,and for the max value the process is the same.

My callDist() function function callDist(type) {

$.getJSON('Dist.json', function(type){
    var $container = $('#parameters').empty();

    $.each(type.distributions, function(i, distributions) {
      if(type==1){

          $.each(distributions.type, function(key, value) {
          $container.append(key + ': ' + value + '<br />');
      }
      //And so on...

    });

    $container.append('<hr />');
  }
  );

 });


 }

And my Dist.Json file is below:

 {
 "distributions":[
  {

     "name":"Uniform",
     "type":"1",
     "parameters":[{ "minValue":"2" , "maxValue":"4" }], 
  },
  {  "name":"Normal",
     "type":"2",
     "parameters":[{ "mean":"5" , "standartDeviation":"3" }],
  },
  {
     "name":"Exponential",
     "type":"3",
     "parameters":[{"lamda":"2"}],
  },
  {
     "name":"Geometric",
     "type":"4",
     "parameters":[{"probability":0.2}],
  }
]
}

How can show the parameters just after the user selects one of the distributions and show the parameters in dynamically created labels and textboxes. Thanks.

aynber
  • 22,380
  • 8
  • 50
  • 63
ntf
  • 1,323
  • 2
  • 12
  • 17
  • You want to show the parameters in the selection of an item, or when you send? – Hodaya Shalom Mar 21 '13 at 11:14
  • @HodayaShalom I want to show the parameters immediately after selection of a distribution.Send is related with someting different.It is for sending the data to a server.The parameters must be shown below the selection and above the send button. – ntf Mar 21 '13 at 11:17
  • Can you explain what you tried to do in function `callDist`? – Hodaya Shalom Mar 21 '13 at 11:23
  • @HodayaShalom It can be false code.But actually I want to get the selected distribution parameters, there are types assigned to the distributions I thougt that they can be used.And the parameters must be passed as key and value.For example, min value as key and value is 2. But I must first check which distr. is selected. – ntf Mar 21 '13 at 11:27
  • Did you check in debugger, if this function runs, in OnChange? (If you can focus your question, it will be better) What is your question? How to take parameters from json? How to put them dynamically in html? How to run the Onchange? – Hodaya Shalom Mar 21 '13 at 11:31
  • @HodayaShalom I didn't check in debugger.I only control from wamp and localhost.Now onChange() isn't working. – ntf Mar 21 '13 at 11:38
  • Did you check this question?: **[Is there an onSelect event or equivalent for HTML – Hodaya Shalom Mar 21 '13 at 11:40
  • @HodayaShalom Yes I checked it but couldn't adjust my problem. – ntf Mar 21 '13 at 11:52

1 Answers1

0

try this code...

function callDist(value)
    {
    $.getJSON('Dist.json', function(type){
        for(i in type.distributions)
         {
            if(type.distributions[i].name==value)
             {
               for( j in type.distributions[i].parameters)
                {  
                   for( k in type.distributions[i].parameters[j])
                   {
                   var val1=distributions[i].parameters[j][k];
                   var val2=distributions[i].parameters[j][k];
                    }

                }

             }


         }



    });
    }
Dineshkumar
  • 351
  • 1
  • 8
  • 1
    This is for only Uniform distribution.Is there any general function for whole distributions parameters? – ntf Mar 21 '13 at 11:51
  • Ok, how can I show these values when clicked from selection?Any idea? – ntf Mar 21 '13 at 12:49
  • crate a label dynamically using jquery like $("#id").append(""); this line add the val1 is selected element specified by the id. – Dineshkumar Mar 21 '13 at 12:51