简体   繁体   English

jQuery启用按钮(如果已选中所有复选框)并将CSS添加到禁用按钮

[英]JQuery enabling button if all checkboxes are selected and add css to disabled button

I am trying to enable a button if all checkboxes are selected and disable it if any are unselected. 我正在尝试启用一个按钮(如果所有复选框均已选中),并禁用它(如果未选中任何复选框)。 I have achived this by using this code: 我已经通过使用以下代码实现了这一点:

checkBoxes.change(function() {
    submitButton.attr("disabled", checkBoxes.is(":not(:checked)"));
 });

What I am not sure about is how to add and remove a css class if the button is enabled or disabled. 我不确定的是,如果启用或禁用了按钮,如何添加和删除css类。

Example here: jsfiddle 此处的示例: jsfiddle

Any help would greatly be appreciated. 任何帮助将不胜感激。

Posting my comment as an answer now... 现在发表我的评论作为答案...

var checkBoxes = $('input.compulsory'),
    submitButton = $('#continue');

checkBoxes.change(function () {
    submitButton.attr("disabled", checkBoxes.is(":not(:checked)"));
    if(checkBoxes.is(":not(:checked)")) {
        submitButton.addClass('disabled');
    } else {
        submitButton.removeClass('disabled');
    }       
});

DEMO HERE 此处演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM