猿问

除了在 cshtml 中提交之外,如何对其他按钮使用验证?

我为 cshtml 编写了一个 html 代码,其中包含两个按钮保存和更新:


 <div class="modal-body">

                    @using (Html.BeginForm("Create", "CustomActivity", FormMethod.Post, new { role = "form", @id = "customActivityForm", @class = "activityForm" }))

                    {

                        <div>

                            <div class="container" id="activity">

                                @Html.LabelFor(m => m.AdminCustomActivity.Activity, new { @class = "" })

                                @Html.TextBoxFor(m => m.AdminCustomActivity.Activity, new { @class = "w100p mb0" })

                                @Html.ValidationMessageFor(m => m.AdminCustomActivity.Activity, "", new { @class = "text-danger" })

                            </div>


                            <div class="container">

                                <div class="column one-fourth">

                                    <div>

                                        @Html.LabelFor(m => m.AdminCustomActivity.Rate, new { @class = "wcrate" })

                                        @Html.TextBoxFor(m => m.AdminCustomActivity.Rate, new { @class = "w100p mb0" })

                                        @Html.ValidationMessageFor(m => m.AdminCustomActivity.Rate, "", new { @class = "text-danger" })

                                    </div>

                                </div>


                            </div>

现在在页脚中有两个按钮添加和更新验证非常适合添加按钮但验证不适用于更新按钮因为更新按钮的函数调用来自 jquery

我如何允许在与添加按钮相同的更新按钮中检查验证。我尝试使用type='submit'但那不起作用


元芳怎么了
浏览 119回答 1
1回答

料青山看我应如是

您必须调用一个实现jQuery 验证的函数才能在单击“更新”按钮时显示验证消息:function validateUpdate() {&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; $("#customActivityForm").validate({&nbsp; &nbsp; rules: {&nbsp; &nbsp; &nbsp; &nbsp; AdminCustomActivity_Activity: "required",&nbsp; &nbsp; &nbsp; &nbsp; AdminCustomActivity_Rate: "required"&nbsp; &nbsp; &nbsp;}&nbsp; });}$("#updateActivity").click(function () {&nbsp; &nbsp; &nbsp; validateUpdate();&nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: "/CustomActivity/Update",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WorkOrderId: @Model.Id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; location.reload();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.scrollTo(0, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error: function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//alert(erro.data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;});})
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答