我想使用object填充下拉列表选项。例如我有对象
{"877":["email1@email.com"],"905":["email2@email.com"],"967":["email3@email.com"],"2":["email3@email.com","email5@email.com"]}
此下拉列表将取决于另一个下拉列表,在通过下拉列表选择公司时,此电子邮件下拉列表将根据所选公司代码填充。 877,905,967,2是公司代码。
答案 0 :(得分:2)
我认为你希望第二次下拉的内容根据第一次下载的内容而改变。如果是这种情况,我会将其作为follows:
HTML:
Company code: <select id="cc"></select>
<br/><br/>
Email: <select id="email"></select>
代码:
$(document).ready(function() {
var data = {
"877":["email1@email.com"],
"905":["email2@email.com"],
"967":["email3@email.com"],
"2":["email3@email.com","email4@email.com"]};
$("#cc").change(function() {
$("#email").empty();
$.map(data[$(this).children(":selected").val()],
function(val) {
$("#email").append($("<option></option>").val(val).html(val));
});
});
for (var id in data) {
$("#cc").append($("<option></option>")
.val(id).html(id));
}
$("#cc").trigger("change");
});
答案 1 :(得分:0)
也许不是更快的解决方案,但有效:
<强> CODE:强>
var obj = {
"877": ["email1@email.com"],
"905": ["email2@email.com"],
"967": ["email3@email.com"],
"2": ["email3@email.com", "email5@email.com"]
};
var select = $('<select></select>');
$.each(obj, function (index, val) {
if (obj[index].length > 1) {
$.each(obj[index], function(index2, val2){
var option = $('<option value="' + index + '">' + val2 + '</option>');
select.append(option[0]);
});
} else {
var option = $('<option value="' + index + '">' + val + '</option>');
select.append(option[0]);
}
})
$('body').html(select);
<强> DEMO 强>
答案 2 :(得分:0)
$(document).ready(function() {
var data = {
"-- Select Date --":["-- Select Date First Then Slot --"],
"06-Dec-2013":["10:00-12:00","2:00-4:00"],
"07-Dec-2013":["14:00-16:00"]};
$("#exmDtId").change(function() {
$("#exmSlotId").empty();
$.map(data[$(this).children(":selected").val()],
function(val) {
$("#exmSlotId").append($("<option></option>").val(val).html(val));
});
});
for (var id in data) {
$("#exmDtId").append($("<option></option>")
.val(id).html(id));
}
$("#exmSlotId").append($("<option></option>")
.val("-- Select Date First Then Slot --").html("-- Select Date First Then Slot --"));
//$("#exmDtId").trigger("change");
});
<table width="100%" border="0" cellpadding="5">
<tr>
<td width="30" align="center">
<SPAN class="caption"><font color="red">*</font> </SPAN>
</td>
<td width="220">
Exam Date
</td>
<td width="350">
<select id="exmDtId"></select>
</td>
</tr>
<tr>
<td width="30" align="center">
<SPAN class="caption"><font color="red">*</font> </SPAN>
</td>
<td>
Exam Batch
</td>
<td>
<select id='exmSlotId' name='exmSlot'></select>
</td>
</tr>
</table>