简体   繁体   English

如何使用jquery禁用提交按钮?

[英]How to disable submit button with jquery?

I have a checkbox which needs to be checked in order for it to be possible to submit the form. 我有一个需要检查的复选框,以便可以提交表格。

So I set the submit button disabled attribute to "disabled". 所以我将提交按钮禁用属性设置为“禁用”。 Then I tried binding the checkbox to it like this so it would enable/disable the submit button. 然后我尝试将复选框绑定到它,这样它将启用/禁用提交按钮。 It does not work: 这是行不通的:

$('input[type=checkbox]#confirm').click(function() {
    if ($(this).is(':checked')) {
        $('#submitButton').removeAttr('disabled');
    } else {
        $('#submitButton').attr('disabled', 'disabled');
    }
});

The submit button stays disabled. 提交按钮保持禁用状态。

HTML: HTML:

<form enctype="application/x-www-form-urlencoded" method="post" action="/controller/action"><dl class="zend_form">
<dd id="confirm-element">
<input type="hidden" name="confirm" value="0"><input type="checkbox" name="confirm" id="confirm" value="1" class="input-checkbox"></dd>
<dt id="confirm-label"><label for="confirm" class="required">Lorem ipsum.</label></dt>
<dt id="submitButton-label">&#160;</dt><dd id="submitButton-element">
<input type="submit" name="submitButton" id="submitButton" value="Potvrdiť" class="input-submit" disabled="disabled"></dd>

<input type="hidden" name="csrf_token" value="ed3e9347145a3eb3d58c4c21d813df26" id=""></dl></form>

try 尝试

$('#confirm').change(function() {
   $('#submitButton').attr('disabled', !this.checked);
});

according to comments below, now this should fixed your problem. 根据下面的评论,现在这应该解决你的问题。

$('#confirm').change(function() {
   $('#submitButton').button( "option", "disabled", !this.checked );
});
$("#submitButton").attr("disabled", true);

您可以禁用该按钮,但仍然可以单击该按钮,因此您必须从按钮取消绑定 click事件:

$("#submitButton").attr('disabled', 'disabled').unbind('click');

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

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