问题
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