0

I have a form with multiple submit buttons. I simply want to disable them once either is pressed, and everything works fine until I 'disable' them in the .submit() JQuery handler, in which case the form no longer appears to send the button name/value information along with the form. How can I disable the buttons but still have the button name/value submitted so I can determine which one was pressed?

The Form

<form action="myaction" method="post" name="sendgift" id="sendgift" > 
    ....input, etc ....       
    <input type="submit" name="_action_go" value="Looks great! Do it" id="sendgiftbtn" />
    <input type="submit" name="_action_index" value="I need to make changes" id="gobackbtn" />
</form>

The JS:

<script type="text/javascript">
    $("#sendgift").submit(function() {

       //if I remove these two lines, everything works
       $("#sendgiftbtn").attr('disabled', 'disabled');
       $("#gobackbtn").attr('disabled', 'disabled');

        console.log("disable buttons called");
        return true;
    });


</script>

2 Answers 2

2

The disabled attribute removes the element from form submission by design: http://www.w3schools.com/tags/att_input_disabled.asp

3 approaches:

  1. If you want to make it "appear" that the form element is disabled you can have js store the value of the field in a hidden variable <input type='hidden' value='[the value of the disabled field]' name='[name of disabled field]' /> and then disable the visible input.

  2. You can also try styling the field as disabled and add an onfocus event that blur()s the field any time a user tries to modify it again.

  3. You can add an onsubmit event to the form that un-disables any inputs with the disabled attribute set, so that they are submitted as normal.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, important to make note that this behavior is by design in HTML, not jQuery specific.
1

In that case instead of disabling the buttons add a disabled class and check for this class whenever you submit the form. If the class is present just return false else true and before returning true add the disabledclass to all the buttons of type submit.

 $("#sendgift").submit(function(e) {

        if(!$(e.target).hasClass('disabled')){
            //if I remove these two lines, everything works
            //$("#sendgiftbtn").attr('disabled', 'disabled');
            //$("#gobackbtn").attr('disabled', 'disabled');

            //console.log("disable buttons called");

            $(this).find('input[type=submit]').addClass('disabled');

            return true;
        }
        return false;
    });

Defined the disabled button style as

.disabled{
  padding: 0px 6px 0px 6px;
  border: 2px outset ButtonFace;
  color: GrayText;
  cursor: inherit;
}

4 Comments

This doesn't change the appearance of the buttons however.
You can make the button look disabled by setting styles in disabled class. I have add the required styles to my answer hope that helps.
Ahhh, that makes more sense. Thanks.
Ended up going with this solution. Thnx.

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.