jquery - 多个选择的选定选项的长度始终返回 1

标签 jquery html

有一个多重选择元素:

<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/

相关文章:

javascript - 宽度 : 100% is not working on children div if parent div has width: calc(100% - 300px)?

javascript - 侧滑菜单选项卡

HTML/CSS 内容缩放

javascript - this.form 没有在 JavaScript 函数中传递表单

javascript - 布局类似于 google chrome 的 tweetdeck 应用程序

javascript - 在 jquery.colorfy 之后设置 Textarea 的值

jquery - 可排序的更改 div 取决于索引

javascript - 滚动事件在移动设备上不起作用

jquery - 防止 ASP.NET MVC bundle 加载多次

javascript - 如何在数据表中显示条形图