在Javascript中,是否可以将多个json(完全是json,而不是单独的一些值)添加到下拉列表中? 比如,我们有两个这样的json文件:
var data = [
{date: "A", quantity: 2, total: 190, tip: 100, type: "tab"},
{date: "B", quantity: 2, total: 190, tip: 100, type: "tab"},
{date: "C", quantity: 1, total: 300, tip: 200, type: "visa"},
{date: "D", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "E", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "F", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "G", quantity: 1, total: 100, tip: 0, type: "cash"},
{date: "H", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "I", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "J", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "K", quantity: 2, total: 200, tip: 0, type: "cash"},
{date: "L", quantity: 1, total: 200, tip: 100, type: "visa"}
];
var data1 = [
{date: "m", quantity: 2, total: 190, tip: 100, type: "tab"},
{date: "n", quantity: 2, total: 190, tip: 100, type: "tab"},
{date: "o", quantity: 1, total: 300, tip: 200, type: "visa"},
{date: "p", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "q", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "r", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "s", quantity: 1, total: 100, tip: 0, type: "cash"},
{date: "t", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "u", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "v", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "w", quantity: 2, total: 200, tip: 0, type: "cash"},
{date: "x", quantity: 1, total: 200, tip: 100, type: "visa"}
];
现在,假设我们创建了两个下拉列表,如下所示:
<select onchange = "??" id="data" class="wrapper-dropdown">
<option value="data">data</option>
<option value="data1">data1</option>
</select>
<select onchange="??" id="total" class="wrapper-dropdown">
<option value ="190">190</option>
<option value ="90">90</option>
<option value ="200">200</option>
<option value ="100">100</option>
<option value ="300">300</option>
</select>
应该放置什么而不是&#34; ??&#34;使其工作,以便每当触摸任何一个下拉菜单时,所选json的过滤值将被打印到控制台?
答案 0 :(得分:0)
我们这段代码http://jsfiddle.net/FeN7g/
$( "#total" ).change(function() {
var selectedvalue=$("#total").val();
$.each(data1,function(i,eacitem)
{
if(eacitem.total == selectedvalue)
{
console.log(eacitem);
}
});
})
答案 1 :(得分:0)
迭代选定的json对象并找到解决问题的必要性
Javascript
for (var i = 0; i < obj.length; i++) {
if (obj[i].total == total) {
document.getElementById("json").innerHTML += JSON.stringify(obj[i]);
}
}
希望有所帮助
答案 2 :(得分:0)
尝试下面给出的小提琴
document.getElementById("json").innerHTML = JSON.stringify(obj);