我试图根据从listbox获得的值将json值设置为选定

我的问题是,在放置静态数据时,该值已被选定。

请看这个
http://jsfiddle.net/9Stxb/89/
但是在上面的代码中,如果我的json数据发生更改,则上述选择将不正确,有时是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);
});

10-06 07:36