dimanche 29 mars 2015

jQuery .prop('checked', true) don't work

I have a set of simple checkboxes, that can check multiple option, but I wish it to behave like a radio button where there will always be one option that is checked.


as you can see in my code, the checkbox will checked, and than uncheck. please let me know what I did wrong. Thanks





$('input[type=checkbox]').on('click', function(event) {
event.preventDefault();
if($('input[type=checkbox]:checked').length === 1 && $(this).is(':checked')) {
return false;
// there is only one checked AND the checked one is $this and we want to prevent it from uncheck
} else {
$(this).prop('checked', true);
alert($(this).prop('checked')); // this return true
}

});



<script src="http://ift.tt/1g1yFpr"></script>
<input type="checkbox" checked="checked"/>
<input type="checkbox"/>
<input type="checkbox"/>






Aucun commentaire:

Enregistrer un commentaire