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