问问题
338 次
1 回答
0
向选项添加自定义属性并在整个组中保持相同的值。
<select name="test" class="select" id="test" multiple="multiple" style="width: 100%;">
<option data-index="1" value="1" class="first">1</option>
<option data-index="1" value="2" class="first">2</option>
<option data-index="1" value="3" class="first">3</option>
<option data-index="2" value="a" id="1" class="hidden second">a</option>
<option data-index="2" value="b" id="1" class="hidden second">b</option>
<option data-index="2" value="c" id="1" class="hidden second">c</option>
<option data-index="2" value="x" id="2" class="hidden second">x</option>
<option data-index="2" value="y" id="2" class="hidden second">y</option>
<option data-index="2" value="z" id="2" class="hidden second">z</option>
<option data-index="2" value="baz" id="3" class="hidden second">baz</option>
<option data-index="2" value="bar" id="3" class="hidden second">bar</option>
<option data-index="2" value="foo" id="3" class="hidden second">foo</option>
</select>
$("select#test").change(function () {
//get the data-index attribute of the selected option
var nIndex = $('option[value= '+ $(this).val()+']').attr('data-index');
//get all elements with the attribute data-index = nIndex
$('option[data-index= '+ nIndex +']').removeClass("hidden");
$(".first").addClass("hidden");
});
于 2013-03-27T19:39:30.647 回答