mardi 30 juin 2015

jquery double counts checked boxes

My code is meant to get all of the checkboxes with name attribute "repo_checkbox" and show an alert with the value of each one that is checked. The problem that I am having is that checkboxes with the checked attribute are getting double counted by jquery and popping up once if they get unchecked and twice if they are left checked.

This is the jquery that gets the checked checkboxes

$(document).ready(function(){
    $('#filter_repos').on('click', function(e){
        $("input:checkbox[name='repo_checkbox']").each(function() {
            if ($(this).is(':checked')) 
            {
                alert(this.value);
            }
        });
    });
});

These are the input tags

<input type="checkbox" name="repo_checkbox" value="5" checked=true>5<br>
<input type="checkbox" name="repo_checkbox" value="6" checked=true>6<br>
<input type="checkbox" name="repo_checkbox" value="7">7<br>
<input type="checkbox" name="repo_checkbox" value="8">8<br>
<input type="submit" value="Filter Repos" id="filter_repos">

The ones without "checked=true" work fine, but the ones with it are counted once more than they should be by jquery.




Aucun commentaire:

Enregistrer un commentaire