【发布时间】:2012-06-03 16:11:56
【问题描述】:
这是我的简单标记:
<select id="firstSelect">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
<select id="secondSelect"></select>
在我的 js 中,有三个不同的数组,名称是渐进式的,如下所示:
var arrayColours1 = new Array('yellow', 'blue');
var arrayColours2 = new Array('red', 'grey');
var arrayColours3 = new Array('white', 'black');
现在我想根据第一个选择的选定选项,用数组中的一个值填充第二个选择,这样:
$('#firstSelect').change(function(){
var selectedValue = $(this).children('option:selected').val();
var elem = 'arrayColours'+ selectedValue;
$.each(elem, function (index, value) {
$('<option/>').val(value).html(value).appendTo('#secondSelect');
});
});
但这不起作用,因为elem 现在是一个字符串。
如何解决这个问题,将“arrayColours”与后缀连接起来,并使其成为调用数组名称的变量?
非常感谢您的关注,任何帮助将不胜感激。
【问题讨论】: