Jquery toggle event is messing with checkbox value

前端 未结 10 1057
暗喜
暗喜 2020-11-29 02:01

I\'m using Jquery\'s toggle event to do some stuff when a user clicks a checkbox, like this:

$(\'input#myId\').toggle(
function(){
//do stuff  
},
function()         


        
10条回答
  •  情歌与酒
    2020-11-29 02:47

    Use the change event instead of the toggle event, like such:

    $('input#myId').change(function () {
        if ($(this).attr("checked")) {
            //do the stuff that you would do when 'checked'
    
            return;
        }
        //Here do the stuff you want to do when 'unchecked'
    });
    

提交回复
热议问题