如何使用 jquery 获取选定选项的数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3707812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get number of selected options using jquery?
提问by Nick Craver
I have a multiple selection list which have more than 5 options. But i want to restrict the selection of the options to 2 or 3 options. How to do it using jquery? How to get the count of selection options in multiple selection list? I am using jquery validation plugin.
我有一个多选列表,其中有 5 个以上的选项。但我想将选项的选择限制为 2 或 3 个选项。如何使用 jquery 做到这一点?如何获取多选列表中选择选项的数量?我正在使用 jquery 验证插件。
回答by Nick Craver
You can use the :selected
selectorto select them, then get the .length
, like this:
您可以使用该:selected
选择器选择它们,然后获取.length
,就像这样:
var count = $("#mySelect :selected").length;
回答by halfdan
You can get the number of selected items in the multiselect with the following:
您可以使用以下方法获取多选中所选项目的数量:
$('#selectList :selected').length
Where #selectList is the id of your .
其中 #selectList 是您的 .
回答by Prashant Lakhlani
var n = $("input:checked").length;
see this for more detail:
http://api.jquery.com/checked-selector/
var n = $("input:checked").length;
有关更多详细信息,请参阅:http:
//api.jquery.com/checked-selector/
回答by Strabek
For checkboxes I'm using:
对于我正在使用的复选框:
$('input[name="InputName"]:checked').size()