Jquery if checkbox is checked add a class

前端 未结 7 2146
梦如初夏
梦如初夏 2020-12-06 01:17

I am trying to add a class when a checkbox is checked.

My jquery:

$(\'input\').attr(\"checked\").change(function(){
$(\'div.menuitem\').addClass(\"me         


        
7条回答
  •  自闭症患者
    2020-12-06 01:39

    Try this:

    $('input').change(function(){
        if ($(this).is(':checked')) {
            $('div.menuitem').addClass("menuitemshow");
        }
    });
    

    You cannot have a change event on an attribute, instead check the change event of the checkbox itself and run a condition to see if it's been checked.

提交回复
热议问题