jQuery 在复选框选中事件上禁用或启用提交按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6149917/
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 20:28:20  来源:igfitidea点击:

Disable or enable Submit button on Checkbox checked event

jqueryasp.net-mvc-2

提问by Ashes

I want something like this but with a slight change. I want a Button to be enabled or disabled on Checkbox checked event, i.e. when checkbox is checked then and then only button should be enabled otherwise it is disabled. This should be done using jQuery Code not JavaScript.

我想要这样的东西,但略有变化。我希望在复选框选中事件上启用或禁用按钮,即当复选框被选中时,然后只应启用按钮,否则将被禁用。这应该使用 jQuery 代码而不是 JavaScript 来完成。

As this is MVC form so there is no Form ID.

由于这是 MVC 表单,因此没有表单 ID。

回答by Darin Dimitrov

$(function() {
    $('#id_of_your_checkbox').click(function() {
        if ($(this).is(':checked')) {
            $('#id_of_your_button').attr('disabled', 'disabled');
        } else {
            $('#id_of_your_button').removeAttr('disabled');
        }
    });
});

And here's a live demo.

这是一个现场演示

回答by Fred Aerofred

This is old but worked for me with jquery 1.11

这是旧的,但对我有用 jquery 1.11

<script>
$(function() {
    $('#checkbox-id').click(function() {
        if ($(this).is(':checked')) {
            $('#button-id').removeAttr('disabled');
        } else {
            $('#button-id').attr('disabled', 'disabled');
        }
    });
});
</script>

the if instruction swapped with the else instruction and in the button html markup add disabled="disabled"

if 指令与 else 指令交换,并在按钮 html 标记中添加 disabled="disabled"

may help someone

可能会帮助某人

Jquery:

查询:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

回答by ebooyens

Seriously? Why not just:

严重地?为什么不只是:

onclick="$('#submitButton').attr('disabled',$('#checkbox').is(':checked'));"

回答by chhameed

$('#checkbox').click(function(){
    if($(this).is(':checked')){
        $('#submitButton').attr("disabled", "true");
    }else {
        $('#submitButton').removeAttr("disabled");
    }

});

... it works for me ...

... 这个对我有用 ...