mardi 19 décembre 2017

jquery limit checkboxes in divs

I want to limit the number of useable checkboxes in a form. I try it with the following jquery code

var limit = 2;
$('input.checkbox').on('change', function(evt) {
    if($(this).siblings(':checked').length >= limit) {
        this.checked = false;
    }
});

and this works. But if input tag is inside a div tag, the jquery stops working.

example:

<div class='voteContainer'>
    <input name='vote1' value='1' class='checkbox' type='checkbox' />
</div>
<div class='voteContainer'>
    <input name='vote2' value='3' class='checkbox' type='checkbox' />
</div>
<div class='voteContainer'>
    <input name='vote3' value='3' class='checkbox' type='checkbox' />
</div>

how i have to change the jquery to work with this input tags in a div?

many thanks in advance




Aucun commentaire:

Enregistrer un commentaire