1

I've a button within my ASP.Net MVC 5 Razor View

@using (Html.BeginForm("MyAction", "MyController", FormMethod.Post, new { id = "myForm" }))
{
    <button type="submit" class="btn btn_d fl sepV_a" id="btnNotify" name="btnNotify"><span>Notify</span></button>   
}

When it is clicked I call a JQuery function which asks the user whether or not they still wish to continue with the Submit

<script type="text/javascript">
    $(document).ready(function () {
        $("#btnNotify").click(ConfirmNotify);
        function ConfirmNotify() {
            if (confirm('Are you sure you wish to continue?')) {
                $("#btnNotify").attr('disabled', 'disabled');
                return true;
            } else {
                return false;
            }
        }
    });
</script>

If the user clicks OK to confirm with the Submit, I then want to disable the Notify button.

Unfortunately the code above disabled the Notify button when the user clicks OK to the prompt, however, the Submit form does not then occur.

If I swap the lines $("#btnNotify").attr('disabled', 'disabled'); and return true; about to this

function ConfirmNotify() {
    if (confirm('Are you sure you wish to continue?')) {
        return true;
        $("#btnNotify").attr('disabled', 'disabled');
    } else {
        return false;
    }
}

The Form gets Submitted when the user clicks OK, but the button Notify doesn't get disabled, i.e., the user can then click the Notify button multiple times.

Does anyone know how to correct this? Any advice greatly appreciated.

Thanks.

tcode
  • 4,787
  • 19
  • 59
  • 117
  • 1
    wherever you are calling this function 'ConfirmNotify' ,make the notify button disable after that function is being called . – Tushar Raj Jun 12 '15 at 15:29

4 Answers4

2

If you want to take an action when a form is submitted - even a single-button form like this one - you should handle the submit event for the form itself, not the click event on the pushbutton. This way your action will be taken when the spacebar or Enter key is used to submit the form. Handling the click event will miss these keyboard interactions.

This is also the cause of the form not submitting. As you know, clicking on a disabled submit button will not submit a form. But you're disabling the button during the click event, which fires before the submit event. So when the browser gets ready to submit the form as a result of the click, it sees that the submit button is disabled and decides not to submit the form.

A minor point, I'd suggest using event.preventDefault() instead of return false to prevent form submission. Either one will work, but .preventDefault() makes your intent more clear, and it also allows any other event handlers to run. return false is equivalent to calling both .preventDefault() and .stopPropagation().

As long as we're at it, let's have some fun and make this code into a jQuery plugin:

jQuery.fn.confirmSubmit = function( message ) {
    $(this).submit( function( event ) {
        if( confirm(message) ) {
            $(this).find('button[type="submit"]').prop( 'disabled', true );
        } else {
            event.preventDefault();
        }
    });
};

Now you can use this on any form, so your example code would be:

$('#myForm').confirmSubmit( 'Are you sure you wish to continue?' );

If you're using a very old version of jQuery, you'll need to stick with .attr('disabled','disabled') as in your example, but with any newer version it's best to use .prop('disabled',true).

Two other notes:

Ordinary JavaScript function names should begin with a lowercase letter, not uppercase. Only constructor functions should begin with a capital letter.

And be aware that confirm() is not a jQuery function. It is a JavaScript function provided by the browser.

Michael Geary
  • 26,814
  • 8
  • 56
  • 71
0

As stated in this answer, use $("#btnNotify").prop('disabled', true); to change the disabled property.

Community
  • 1
  • 1
Thomas F
  • 1,399
  • 2
  • 23
  • 23
  • I've no problem disabling the button, my code above already does that. It's the submit form I'm having trouble with. – tcode Jun 12 '15 at 15:38
  • So you can't submit after you disable the submit button? – Thomas F Jun 12 '15 at 15:39
  • Could you do `$("#myForm").submit();` after the disable, then `return false` in the first case as well? – Thomas F Jun 12 '15 at 15:44
  • Now it looks like I copied your answer, but I hadn't refreshed the page since you posted. Shouldn't you still return `false` after the submit as well? – Thomas F Jun 12 '15 at 15:50
  • Why would I do that? If the user wants to submit the form returning false with prevent the submit? – tcode Jun 12 '15 at 15:54
  • Anyway, as I can't comment on any post (low rep), in response to the comments on the question, why don't you just `$("#myForm").submit(function(){$("#btnNotify").prop('disabled',true);});' – Thomas F Jun 12 '15 at 15:55
  • The `return false` was for the button click, as the form was already submitted, (I think) this would prevent any possible double submitting. (although I think that disabling the button would also stop the click) – Thomas F Jun 12 '15 at 16:00
0

I found what I thought to be the answer to my own question, and although the code below works, it isn't the best answer. That was kindly provided by Michael Geary. Please see accepted answer.

<script type="text/javascript">
    $(document).ready(function () {
        $("#btnNotify").click(ConfirmNotify);
        function ConfirmNotify() {
            if (confirm('All available locums will be contacted via text and email. Are you sure you wish to continue?')) {
                $("#btnNotify").attr('disabled', 'disabled');
                $('#myForm').submit();
            } else {
                return false;
            }
        }
    });
</script>

The line $('#myForm').submit(); is what was needed.

tcode
  • 4,787
  • 19
  • 59
  • 117
  • Not sure this is a cross browser solution, some browsers could submit then the FORM twice. This needs more testing – A. Wolff Jun 12 '15 at 15:42
  • Something is fishy here. What happens if you remove this entire script? Does the form submit properly or not? – Michael Geary Jun 12 '15 at 15:49
  • @A.Wolff Thanks for the input. I'll test on other browsers (Chrome is my development browser and all working fine within it). Do you know of any other solution to my problem? Thanks. – tcode Jun 12 '15 at 15:50
  • @MichaelGeary If I remove the entire Script, once the Notify button is clicked, the form submits to the HttpPost Controller as expected. – tcode Jun 12 '15 at 15:52
  • Like here maybe http://stackoverflow.com/questions/926816/how-to-prevent-form-from-submitting-multiple-times-from-client-side And still be aware a FORM can be submitted without clicking submit button – A. Wolff Jun 12 '15 at 15:56
  • 1
    The problem is that you're disabling the button on the `click` event. This event fires before the form is submitted (obviously) - but then when the browser gets around to thinking about submitting the form, the button is already disabled and it doesn't submit it. Handle the `submit` event on the form itself instead and it will work properly. This will also handle keyboard form submission as well as mouse clicks. See my answer for details. – Michael Geary Jun 12 '15 at 16:41
0

Starting with the solution kindly provided by Michael Geary, here is one including custom confirmation message as button attribute:

<button class='once-only' confirm='Please confirm you wish to proceed' type="submit"> GO </button>

$(document).ready(function () {
    $(".once-only").click(ConfirmNotify);
    function ConfirmNotify() {
        if (confirm($(this).attr('confirm'))) {
           this.form.submit();
           this.disabled = true;
           return true;
       } else {
        return false;
    }
}
});

user3251285
  • 131
  • 7