jQuery中
与ListBox的工作想设置listbox
值基于从json
jQuery中
我的问题得到的值作为选择的是,同时使静态数据正在逐渐作为选择的值。
请看看这个 http://jsfiddle.net/9Stxb/89/ 但是在上面的代码,如果我的json
数据的变化,上述选择不会correct.this有时将被val1,val2,val3
或任意组合。
我用下面的代码
,但它不工作.. http://jsfiddle.net/9Stxb/91/
代码
var myData ={ "_id": "525f8226360cc02c2bd63dec",
"id": "30",
"jobs": [{
"_id": "524a4f4cc973602da0d4ee10",
"id": "4",
"launch": "mypraram val url",
"names": "val1",
}, {
"_id": "525f6deb360cc02c2bd63dea",
"id": "28",
"launch": "jithinurl",
"names": "val2"
}],
"server": "180.16.17.60"
}
$(function() {
for(x in mydata.jobs){ $('#jobSel').val(['mydata.jobs[x].names','mydata.jobs[x].names']);
}
});
HTML
<div class="id_100"> <select id="jobSel" class="longcombo" multiple>
<option value="val1">Val 1</option>
<option value="val2">Val 2</option>
<option value="val3">Val 3</option>
</select>
</div>
回答:
这里的更新JS ...
Demo Fiddle
var jsonData = { "_id": "525f8226360cc02c2bd63dec",
"id": "30",
"jobs": [{
"_id": "524a4f4cc973602da0d4ee10",
"id": "4",
"launch": "mypraram val url",
"names": "val1",
}, {
"_id": "525f6deb360cc02c2bd63dea",
"id": "28",
"launch": "jithinurl",
"names": "val2"
}],
"server": "180.16.17.60"
}
$(function() {
var selectedVals = new Array();
for (var i=0;i<jsonData.jobs.length;i++) {
console.log(jsonData.jobs[i])
selectedVals.push(jsonData.jobs[i].names);
}
$('#jobSel').val(selectedVals);
});
以上是 jQuery中 的全部内容, 来源链接: utcz.com/qa/257969.html