简体   繁体   English

如何将选择值传递给另一个函数,并根据传递的值从Json获取数据?

[英]How can I pass select value to another function and get data from Json according to passed value?

I am writing html and javascript code to get some data from a Json file. 我正在编写html和javascript代码以从Json文件中获取一些数据。 My html code: 我的html代码:

 <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) { 我的callDist()函数功能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: 我的Dist.Json文件如下:

 {
 "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. 谢谢。

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];
                    }

                }

             }


         }



    });
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从函数到 json 值获取结果? - How can I get the result from function to json value? 如何基于传递给函数的值从javascript中的数据库中获取数据 - How to get data from database in javascript based on the value passed to the function 如何从后面的代码中获取ID传递给javascript函数的控件的值 - How can I get the value of a control whose ID was passed to javascript function from code behind 如何将值从函数传递到另一个文件中的另一个组件 - How can I pass the value from my function to another component in another file 在JavaScript中,如何将值从未命名函数传递给另一个未命名函数? - In JavaScript, how can I pass value from unnamed function to another unnamed function? 如何将Promise的值传递给另一个函数? - How can I pass the value of a Promise to another Function? 如何从异步 function 获取数据并将其用作另一个异步 function 上的 json 的属性值? - How to get the data from a async function and used that as a property value for a json on another asyn function? 如何从JSON URL中获取特定值,并将其传递给Javascript变量? - How can I get a specific value from a JSON URL, and pass it into a Javascript variable? 如何使用Javascript从下拉列表中选择一个值并将该值作为var传递给另一函数? - How does one use Javascript to select a value from a drop down list and pass that value as a var to another function? 我如何将值从组件传递到ReactJS中的另一个组件 - How can i pass a value from a component to another component in ReactJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM