How do I check if a checkbox is checked with JQuery?

前端 未结 2 1674
长情又很酷
长情又很酷 2020-12-18 12:07

I am trying to allow a click function if the user has checked a checkbox on the page. Otherwise, I want to add a class to the #additional_foreign button.

I think I

2条回答
  •  旧巷少年郎
    2020-12-18 12:37

    The problem is that the code doesn't run continually, only when the page loads, when all the boxes are unchecked. You need an action that fires when the box is checked or unchecked, which adds an action or a class to the button:

    $('#foreign_checkbox').change(function(){
        if (this.checked){
            $('#additional_foreign').click(function() {
               doSomething();
            });
        } else {
            $('#additional_foreign').addClass('btn_disable');
        }   
    });
    

提交回复
热议问题