4

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>
2
  • Setting the submit button to disabled might not be enough. Consider preventing the default submit event of the form (in some browsers pressing enter in a simple input text box also submits the form). Commented Jan 13, 2011 at 7:51
  • @bazmegakapa That's not needed. I have that secured at the server side if someone tries to go around the javascript. I just need to make it visually visible that the form can only be submitted after the checkbox input has been checked. Commented Jan 13, 2011 at 7:52

3 Answers 3

1

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 );
});
Sign up to request clarification or add additional context in comments.

1 Comment

hmmm I made a type there. and lonesomeday already fixed it. it should work... here's a demo
0
$("#submitButton").attr("disabled", true);

Comments

0

You can disable the button, but you still have a click on the button so you have to unbind the click event from the button:

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

5 Comments

That's not my problem. My issue is that the submit button stays disabled.
You cannot click on a disabled button anyways.
if tested your script, and it runs aswel.. but you want a normal form post? try to post form with .submit() on the click event from the button
Ehm. Read my question. I want to disable/enable the submti button with the checkbox. That's means setting/unsetting its disabled attribute.
like i said, i tested you script you posted. it works fine.. i tested this: pastie.org/1455449

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.