-2

I have these checkboxes:

<input type="checkbox" class="ids" name="ids[]" value="2">
<input type="checkbox" class="ids" name="ids[]" value="3">
<input type="checkbox" class="ids" name="ids[]" value="4">
<input type="checkbox" class="ids" name="ids[]" value="5">
<input type="checkbox" class="ids" name="ids[]" value="6">

My question is via jquery, how would I loop through the ids[] on form submit?

$("#form").submit(function(e) {
  //Loop throught ids[] 
});

I have tried this:

$('input[type=checkbox][name=ids[]]').each(function () {   
  console.log("Here");
});

But it didn't work

Mohammad
  • 21,175
  • 15
  • 55
  • 84
user979331
  • 11,039
  • 73
  • 223
  • 418

1 Answers1

2

Your selector is invalid. You should escape [] with \\.

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[\]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\. documentation

$('input[type=checkbox][name=ids\\[\\]]').each(function(){
  console.log(this.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="ids" name="ids[]" value="2">
<input type="checkbox" class="ids" name="ids[]" value="3">
<input type="checkbox" class="ids" name="ids[]" value="4">
<input type="checkbox" class="ids" name="ids[]" value="5">
<input type="checkbox" class="ids" name="ids[]" value="6">
Mohammad
  • 21,175
  • 15
  • 55
  • 84