开发者

How to get number of selected options using jquery?

开发者 https://www.devze.com 2023-01-16 05:56 出处:网络
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 o

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.


You can use the :selected selector to select them, then get the .length, like this:

var count = $("#mySelect :selected").length;


You can get the number of selected items in the multiselect with the following:

$('#selectList :selected').length

Where #selectList is the id of your .


var n = $("input:checked").length; see this for more detail: http://api.jquery.com/checked-selector/


For checkboxes I'm using:

$('input[name="InputName"]:checked').size()


I use select2 for multi select and this worked with me

 $("#selectedItems").val().length;

note that 'selectedItems' is the id of multiselect list

0

精彩评论

暂无评论...
验证码 换一张
取 消