Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show a dialog before submitting?

I've got a form with a submit button, and I want to show a dialog before continuing the form submission.

The code below is showing the dialog properly, but it continues the submit anyway:

@using (Html.BeginForm("SubmitForm", "Home", FormMethod.Post, new { id = "form" }))
{
    @Html.AntiForgeryToken()
    *snip*

    <div class="collapseClosed panel-footer panel-collapse collapse in">
        <input type="submit" name="btnSubmit" id="btnSubmit" class="btn btn-success" value="Submit" />
    </div>
}
<div id="confirm" title="Confirm">
    <span id="confirmtext"></span>
</div>

<script>
    $("#btnSubmit").on("click", function () {
        currentForm = $(this).closest('form');
        $("#confirmtext").text("Please confirm.");
        $("#confirm").dialog('open');
    });
</script>
like image 283
Martin Avatar asked May 28 '15 08:05

Martin


2 Answers

You need to add e.preventDefault(); inside the JQuery click event to prevent submitting.

<script>
    $("#btnSubmit").on("click", function (e) {
        e.preventDefault();
        currentForm = $(this).closest('form');
        $("#confirmtext").text("Please confirm.");
        $("#confirm").dialog('open');
    });
</script>
like image 74
Nic Avatar answered Sep 22 '22 12:09

Nic


<script>
    $("#btnSubmit").on("click", function (e) {
        e.preventDefault();
        currentForm = $(this).closest('form');
        $("#confirmtext").text(Please confirm.");
        $("#confirm").dialog('open');
    });
</script>
like image 39
Danish Bhayani Avatar answered Sep 23 '22 12:09

Danish Bhayani