【问题标题】:Java script confirmation form submit MVCJavascript确认表单提交MVC
【发布时间】:2019-03-12 20:30:42
【问题描述】:

我试图调用 java 脚本确认表单提交,但它跳过它。即使我想先运行 java 脚本进行确认,它也会直接调用控制器。 我不知道我做错了什么。 JV Scripts 还是新手。

HTML

@model WMS_Web.Models.FileMaintenance.PrincipalModels
@section Scripts {
    @Scripts.Render("~/Script/FileMaintenance/CommonFunction.js")
}
@{
    ViewBag.Title = "PrincipalModel";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Principal</h2>

@using (Html.BeginForm("Create","Principal",FormMethod.Post)) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.CompanyId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CompanyId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CompanyId, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <button onclick='functionConfirm("Do you like Football?", function yes() {
         alert("Yes")
      },
      function no() {
         alert("no")
      });'>XXX</button>


            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back", "ViewPrincipal")
</div>

Java 脚本

function functionConfirm(msg, myYes, myNo) {
    var confirmBox = $("#confirm");
    confirmBox.find(".message").text(msg);
    confirmBox.find(".yes,.no").unbind().click(function () {
        confirmBox.hide();
    });
    confirmBox.find(".yes").click(myYes);
    confirmBox.find(".no").click(myNo);
    confirmBox.show();
}

【问题讨论】:

标签: javascript c# html asp.net-mvc


【解决方案1】:

你可以试试这个:

@using (Html.BeginForm("Create","Principal",FormMethod.Post)) 
{
   @Html.AntiForgeryToken()
   ...
   <input type="submit" name="name" value="Save" onclick="javascript: return SubmitForm();" />
} 

Javascript

function SubmitForm() {
   var r = confirm("Are you sure you want to submit?");
    if (r == false) {
      return false;
    }
    // do something
    return true;       
}

【讨论】:

    猜你喜欢
    • 2016-10-19
    • 2019-10-14
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多