mardi 21 mars 2017

jquery checkbox reset - inconsistent operation

I want to use Jquery to reset checkboxes. I've read the Q/A on the topic and came up with this:

<script>
$(document).ready(function(){
    $("#clearall").click(function(){
        alert('clearing checkboxes');
        $('input:checkbox').attr('checked',false);
    });
});
</script>
<input id="cb1" type="checkbox" checked>
<input id="cb2" type="checkbox" checked >
<button id="clearall">Click me</button>

On load, two checkboxes appear checked. Clicking the button clears them. This is as expected. What is NOT expected is that if I check the boxes and click the button, the check boxes do NOT clear. No javascript errors or warnings appear. I've checked the functionality in Firefox, Safari and Chrome - and all 3 operate the same way. I inserted an alert which fires on every attempt, so the function is executing - but the checkboxes aren't getting reset after the first time. Can anyone offer any insight into why the check boxes do NOT clear if set by the user?




Aucun commentaire:

Enregistrer un commentaire