lundi 6 novembre 2017

checkbox change not detected when checkbox is checked remotely

I have two checkboxes. When the first is checked, I force the second to be checked too. When the second is checked, I want to toggle a message. But the change event is not detected on the second when first one is checked:

$(document).ready(function(){
        $("#first").change(function(){
                if ($(this).is(":checked")){
                        $("#second").prop('checked', true).attr("disabled","disabled");
                } else {
                        $("#second").prop('checked', false).removeAttr("disabled");
                }
        });
        
        
        $("#second").change(function(){
                if ($(this).is(":checked")){
                        $("#alert").show();
                } else {
                        $("#alert").hide();
                }
        })
})
<script src="http://ift.tt/1oMJErh"></script>
<input id="first" type="checkbox">first &nbsp;&nbsp;&nbsp;
<input id="second" type="checkbox">second
<br><br>
<div id="alert" style="display:none">This is an alert</div>



Aucun commentaire:

Enregistrer un commentaire