我正在努力为两个相互依赖的下拉列表编写代码,我需要一些帮助:
// Question 1
var test1 = ['|Please Select','1|1','2|2','3|3']
// Question 2
var test2 = []
var test2a = ['|Please Select','1a|1a','2a|2a','3a|3a']
var test2b = ['|Please Select','1b|1b','2b|2b','3b|3b']
var test2c = ['|Please Select','1c|1c','2c|2c','3c|3c']
// Set the options from Question 1 using the values from the array test1
val = "test1";
function add_options(val){
arr = window[val];
var the_id = "#"+val;
$.each(arr, function(key,val){
options = val.split('|');
$(the_id).append(
$('<option></option>')
.val(options[0])
.html(options[1])
);
});
}
我需要帮助
当用户单击 test1 中的选项时,获取选定的选项。
知道所选选项后,我可以为问题 2 构建数组:
if(selected == "1"){
var test2 = test2a;
} else if (selected == "2") {
var test2 = test2b;
} else if (selected == "3") {
var test2 = test2c;
}
val = "test2";
function add_options(val){
arr = window[val];
var the_id = "#"+val;
$.each(arr, function(key,val){
options = val.split('|');
$(the_id).append(
$('<option></option>')
.val(options[0])
.html(options[1])
);
});
}
希望你能帮忙!