在 jQuery 中单击复选框时如何启用按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3565632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:40:20  来源:igfitidea点击:

How to enable button when checkbox clicked in jQuery?

jquery

提问by ktm

How to enable button when checkbox clicked in jQuery?

在 jQuery 中单击复选框时如何启用按钮?

回答by Nick Craver

You can do it like this:

你可以这样做:

$("#checkBoxID").click(function() {
  $("#buttonID").attr("disabled", !this.checked);
});

This enables when checked, and disables again if you uncheck. In jQuery .attr("disabled", bool)takes a boolean, so you can keep this pretty short using the this.checkedDOM property of the checkbox.

这在选中时启用,如果取消选中则再次禁用。在 jQuery 中.attr("disabled", bool)采用一个布尔值,因此您可以使用this.checked复选框的DOM 属性来保持这个非常简短。

回答by Tim

$("#yourcheckboxid").click(function() {
    var checked_status = this.checked;
    if (checked_status == true) {
       $("#yourbuttonid").removeAttr("disabled");
    } else {
       $("#yourbuttonid").attr("disabled", "disabled");
    }
});