简体   繁体   中英

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:

<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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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