我有多个选择,我需要为每个选项中的每个项目提供某种帮助。我已经为每个选择生成了一个包含值的数组,现在我需要选择为每个选择选择正确的数组。示例:
ArrayA[1] = "text";
ArrayA[2] = "text2";
ArrayB[1] = "text";
ArrayB[2] = "text2";
<select id="select1" class="DDL ArrayA">...</select><span class="help">
<select id="select2" class="DDL ArrayB">...</select><span class="help">
我需要将选择框中名为class的数组中的文本放入带有&#34; help&#34;的范围。我怎么能这样做?
这就是我现在所拥有的:
$(this).parent().children().closest('span').text(ARRAY_NAME[$('option:selected', this).val()]);
答案 0 :(得分:0)
您可以使用eval()
函数从相应的select:
示例:FIDDLE
HTML:
<div><select id="SEL1" class="DDL Array">
<option selected value="-1" disabled="disabled">choose something</option>
<option value="1">one</option>
<option value="2">two</option>
</select><span class="help"></span></div>
<div><select id="SEL2" class="DDL Array">
<option selected value="-1" disabled="disabled">choose something</option>
<option value="1">one</option>
<option value="2">two</option>
</select><span class="help"></span></div>
的javascript:
$(function () {
var ArraySEL1=[],ArraySEL2=[];
ArraySEL1[1] = "array 1 text";
ArraySEL1[2] = "array 1 text2";
ArraySEL2[1] = "array 2 text";
ArraySEL2[2] = "array 2 text2";
$("select").change(function () {
var id_of_current_select = $(this).attr("id");
var selected_value = $(this).val();
var the_help_text = eval("Array"+id_of_current_select+"["+selected_value+"]");
$(".help",$(this).parent()).html(the_help_text);
});
});