54

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

mit
  • 11,083
  • 11
  • 50
  • 74
Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236

8 Answers8

81

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/

Baqer Naqvi
  • 6,011
  • 3
  • 50
  • 68
Ram
  • 143,282
  • 16
  • 168
  • 197
20

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();
André
  • 2,042
  • 1
  • 23
  • 26
14

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() );
   });

});
rbtLong
  • 1,542
  • 3
  • 14
  • 31
11
$('[name="CheckboxName"]:checked').each(function () {
    // do stuff
});
Parameswaran
  • 314
  • 3
  • 12
8

I would like to add that if you're trying to get all of the check-boxes with that name that have been selected, you would want to use the following syntax:

$("[name='bla[]']:checked").each(function () {
    // do stuff
});

Make sure there is not a space between the closing ] and :checked

Gaurang Joshi
  • 684
  • 16
  • 32
Bardicer
  • 1,405
  • 4
  • 26
  • 43
6

// get checkbox values using checkbox's name

<head>
        <script>
        function getCheckBoxValues(){
            $('[name="checkname"]').each( function (){
                alert($(this).val());
            });
        }
        </script>
    </head>
    <body>
        <input type="checkbox" name="checkname" value='1'/>
        <input type="checkbox" name="checkname" value='2'/>
        <input type="checkbox" name="checkname" value='3'/>
        <input type="button" value="CheckBoxValues" onclick="getCheckBoxValues()"/>
    </body>

// get only the values witch are checked

function getCheckBoxValues(){
        $('[name="checkname"]').each( function (){
            if($(this).prop('checked') == true){
                alert($(this).val());
            }
        });
    }
swamy
  • 67
  • 1
  • 4
5
$("input[name='bla[]']").each( function () {
    alert($(this).val());
});
Dirty-flow
  • 2,306
  • 11
  • 30
  • 49
4

Like it has been said few times, you need to change your selector to

$("input[name='bla[]']")

But I want to add, you have to use single or double quotes when using [] in selector.

realshadow
  • 2,599
  • 1
  • 20
  • 38