Getting amount of checkboxes checked by name

廉价感情. 提交于 2019-12-12 21:13:08

问题


I'm trying to find out how many checkboxes have been checked but am having some trouble..

My checkboxes are all named delete[].

var count = ($('#form_store_setup input[name=delete]:checked').length);

...and this doesn't work at all:

var count = ($('#form_store_setup input[name=delete[]]:checked').length);

回答1:


Simply wrap delete[] in double quotes in your second example like this:

var count = ($('#form_store_setup input[name="delete[]"]:checked').length);



回答2:


You need to escape the square brackets. Try this:

var count = $('#form_store_setup input[name=delete\\[\\]]:checked').length;

Or put the attribute value in quotes:

var count = $('#form_store_setup input[name="delete[]"]:checked').length;



回答3:


Assuming you do not have any other named items, that begin with delete, you can use the "starts with" matching:

var count = ($('#form_store_setup input[name^=delete]:checked').length);



回答4:


This should do the trick. Try adding single quotes around your input name. Also you can call .size() instead of .length for the same effect.

var count = ($('#form_store_setup input[name='delete[]']:checked').length);

See this document for reference regarding the size() function (http://api.jquery.com/size/)



来源:https://stackoverflow.com/questions/23812040/getting-amount-of-checkboxes-checked-by-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!