質問

Please see the link : http://jsfiddle.net/NaUAL/

Result :

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

Desire Result :

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

I need a way to add value to data and get similar output ... value should be listed in the following data : "base": ["Please choose from above"], "beverages": ["Coffee", "Coke"], "snacks": ["Chips", "Cookies"]

役に立ちましたか?

解決

DEMO

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

assign data key value like optionText_optionValue

and

$.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>')
});

他のヒント

$.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.
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top