我有一个棘手的问题.我正在尝试将第三个参数添加到以下函数/循环中并且已经碰壁了.救命?
$.each(studentselectValues, function(key, value) {
$('select[name="cf_Student"]')
.append($("<option></option>")
.attr("value",key)
.text(value));
});
Run Code Online (Sandbox Code Playgroud)
以下是studentselectValues变量的代码:
studentselectValues = {
'1': 'My 1st Option',
'2': 'My 2nd Option',
'3': 'My 3rd Option'
}
Run Code Online (Sandbox Code Playgroud)
我正在做的是使用上面显示的选项填充选择.我的目标是为每个选择添加第三个属性.有谁知道如何实现这一目标?
提前致谢.
我将studentselectValues哈希变成哈希数组......这样你可以根据需要使用尽可能多的属性:
studentselectValues = [
{ id: '1', key: 'My 1st Option', text: 'First' },
{ id: '2', key: 'My 2nd Option', text: 'Second' },
];
$.each(studentselectValues, function(index, item) {
$('select[name="cf_Student"]')
.append($("<option></option>")
.attr("value",item.key)
.text(item.text));
});
Run Code Online (Sandbox Code Playgroud)