有一个多重选择元素:
<select id="secta_code" name="secta_code[]" multiple="multiple" size="10"></select>
$(document).ready(function() {
$('#secta_code').on('change',function() {
alert("nb = "+$(this,"option:selected").length);
});
});
如何动态捕获处理程序内选定选项的数量?
最佳答案
您可以使用以下替代方法:
$('#secta_code').on('change', function () {
alert("nb = " + $(":selected", this).length);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="secta_code" name="secta_code[]" multiple="multiple" size="10">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
The jQuery constructor accepts a 2nd parameter called context which can be used to override the context of the selection.
关于jquery - 多个选择的选定选项的长度始终返回 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26085614/