0

请查看链接:http: //jsfiddle.net/NaUAL/

结果 :

<select id="second-choice">
<option>Chips</option>
<option>Cookies</option>
</select>

愿望结果:

<select id="second-choice">
<option value="465">Chips</option>
<option value="984">Cookies</option>
</select>

我需要一种为数据添加价值并获得类似输出的方法...值应列在以下数据中:“base”:[“请从上面选择”],“beverages”:[“Coffee”,“Coke” ], "零食": ["薯片", "饼干"]

4

2 回答 2

1

演示

var data = {
        "base" : ["Please choose from above"],
        "beverages": ["Coffee_465","Coke_984"],
        "snacks": ["Chips_123","Cookies_987"]
}

分配数据键值,例如 optionText_optionValue

$.each(vals,function(i,val){
    var v = val.split('_'); //use split to break optionText and optionValue
    html.push('<option value="'+v[1]+'">'+v[0]+'</option>')
});
于 2013-10-01T14:54:58.817 回答
0
$.each(vals,function(i,val)
{
   html.push('<option value='+value_variable+'>'+val+'</option>'); // value variable contains the actual value such as 465 or 984 or whatever.
});
于 2013-10-01T14:48:47.810 回答