studentselectValues = [{
{key:'1', value:'My 1st Option', third:"third value"},
{key:'2', value:'My 2nd Option', third:"third value"},
{key:'3', value:'My 3rd Option', third:"third value"}
}];
$.each(studentselectValues, function(index, data) {
$('select[name="cf_Student"]')
.append($("<option></option>")
.attr("value",data.key)
.text(data.value+ " "+ data.third));
}
在一個旁註,你不應該追加循環中的每個項目:它的資源很大。連接,然後附加。
var studentselectValues = [{
value: '1',
key: 'test',
third: 'hoho'
}, {
value: '2',
key: 'test2',
third: 'test'
}];
var options = [];
$.each(studentselectValues, function() {
options.push('<option value="' + this.key + '">' + this.value + ' ' + this.third + '</option>');
});
var optionsS = options.join();
$('select[name="cf_Student"]').append(optionsS);
爲什麼-1一個正確的答案? – pixeline