ASP.NET MVC - Having a confirmation button with a

2019-04-20 04:07发布

问题:

I have an strongly typed view for my model and what I'd like is that when the user clicks on submit, a confirmation box pop up confirming that the user does indeed wish to submit the form, if they click cancel then it shouldn't fire the HttpPost Action for that View, is this possible?

回答1:

Of course it is possible. I like to use an unobtrusive approach. Here is a simplified example:

jQuery(document).ready(function () {
  jQuery('[data-confirm]').click(function (e) {
    if (!confirm(jQuery(this).attr("data-confirm")))
    {
      e.preventDefault();
    }
  });
});

Then you only need to add a data-confirm attribute to your submit button for example

<input type="submit" data-confirm="are u sure?" />

Of course you can use this attribute on links, buttons, etc. you are not restricted to submit buttons only, and if you want to implement a fancier confirm dialog later than you will have to replace the code only in one place.



回答2:

        function doSubmit()
        {                                 
                if(window.confirm("ARE YOU SURE TO PERFORM THIS ACTION"))
                {                       
                    return true;
                }
                else return false;                
        }

call doSubmit() function on onsubmit event of the form, Eg- onsubmit="return doSubmit()



回答3:

you can add a simply jQuery call for that.

at the end of your view add:

<script type="text/javascript">

    $("form").submit(function() {

        return confirm('Are you sure?');

    });

</script>

or, add a

onsubmit="return confirm('Are you sure?');"

as a new element property



回答4:

I believe this can be done by overriding the submit button using jquery. Jquery .submit()

This way, when the person hits submit you can show a message and either submit it or cancel it.