dimanche 31 mai 2015

jQuery does not get correctly values of all checked checkbox

I have a list of checkboxes initially checked as follow:

<input type="checkbox" name="capacity" value="16" checked> 16 <br>
<input type="checkbox" name="capacity" value="32" checked> 32 <br>
<input type="checkbox" name="capacity" value="64" checked> 64 <br>
<input type="checkbox" name="capacity" value="128" checked> 128 <br>

Each time a checkbox is checked or unchecked, I want to update the capacity list again, therefore, I use (with a little bit of repeating the code):

$('input[name="capacity"]').change(function() {
   var capacity = $('input[name="capacity"]:checked').map(function() {
        return this.value;
    }).get();
   console.log(capacity);
}

However, the capacity still contains value of last unchecked checkbox. More specifically, when I unchecked 128 -> capacity = ["16","32","64","128"]. Then, I unchecked 32 -> capacity =["16","32","64"]

I don't understand this strange behavior or am I doing something wrong?




Aucun commentaire:

Enregistrer un commentaire