[英]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.