Get checkbox values using checkbox name using jquery
I have several input checkboxes, (they name is same for send array on server).
So, I need get each value this checkboxes and I want use as selector checkbox names, this not works, help please.
<form>
<input type="checkbox" name="bla[]" value="1" />
<input type="checkbox" name="bla[]" value="2" />
</form>
js:
$(document).ready( function () {
$("input[name=bla]").each( function () {
alert( $(this).val() );
});
});
DEMO
Solution 1:
You are selecting inputs with name attribute of "bla"
, but your inputs have "bla[]"
name attribute.
$("input[name='bla[]']").each(function (index, obj) {
// loop all checked items
});
http://jsfiddle.net/26axX/
Solution 2:
If you like to get a list of all values of checked checkboxes (e.g. to send them as a list in one AJAX call to the server), you could get that list with:
var list = $("input[name='bla[]']:checked").map(function () {
return this.value;
}).get();
Solution 3:
You should include the brackets as well . . .
<input type="checkbox" name="bla[]" value="1" />
therefore referencing it should be as be name='bla[]'
$(document).ready( function () {
$("input[name='bla[]']").each( function () {
alert( $(this).val() );
});
});