简体   繁体   English

jQuery表单仅提交一秒钟的单击

[英]Jquery form submitting only one second click

I have a form in .NET MVC that can be submitted by multiple buttons. 我在.NET MVC中有一个可以通过多个按钮提交的表单。 Each button set the eventCommand hidden field to a specific value then submits the form like so: 每个按钮都将eventCommand隐藏字段设置为特定值,然后提交表单,如下所示:

$(document).ready(function(){
    $("[data-crm-action]").on("click", function (e) {
        e.preventDefault();
        $("#EventCommand").val($(this).data("crm-action"));
        $("form").submit();
    });
});

However, I need to click twice on a button to see the form submitted. 但是,我需要在按钮上单击两次以查看提交的表单。 Why doesn't it submit on the first click? 为什么没有在第一次点击时提交? At the first click, the cursor is sent back to the text box for the search field. 第一次单击时,光标将被发送回搜索字段的文本框。 The email field is tagged required in the model with annotations but i tried to add formnovalidate to my buttons and it still does it. 电子邮件字段在模型中用注释标记为必填项,但我尝试将formnovalidate添加到我的按钮中,但它仍然这样做。

Here is some code from the view: 这是视图中的一些代码:

@using (Html.BeginForm())
{
@Html.HiddenFor(m => m.EventCommand)
@Html.HiddenFor(m => m.IsValid)
@Html.HiddenFor(m => m.Mode)

<!-- BEGIN SEARCH AREA -->
if (Model.IsSearchAreaVisible)
{
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h1 class="panel-title">Search for Users</h1>
        </div>
        <div class="panel-body">
            <div class="form-group">
                @Html.LabelFor(m => m.SearchEntity.Email):
                @Html.TextBoxFor(m => m.SearchEntity.Email, new { @class = "form-control" })
            </div>
        </div>
        <div class="panel-footer">
            <button id="btnSearch" class="btn btn-sm btn-primary" data-crm-action="search">
                <i class="glyphicon glyphicon-search"></i>&nbsp;Search
            </button>
            <button id="btnReset" class="btn btn-sm btn-primary" data-crm-action="resetsearch">
                <i class="glyphicon glyphicon-share-alt"></i>&nbsp;Reset
            </button>
            <button id="btnAdd" class="btn btn-sm btn-success" data-crm-action="add">
                <i class="glyphicon glyphicon-plus"></i>&nbsp;Add
            </button>
        </div>
    </div>
}
<!-- END SEARCH AREA -->

<!-- BEGIN DETAIL AREA -->
if (Model.IsDetailAreaVisible)
{
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h1 class="panel-title">User Information</h1>
        </div>
        <div class="panel-body">
            <!-- BEGIN MESSAGE AREA -->
            <div class="row">
                <div class="col-sm-12">
                    @if (!Model.IsValid)
                    {
                        <div class="alert alert-danger alert-dismissable" role="alert">
                            <button type="button" class="close" data-dissmiss="alert">
                                <span aria-hidden="true">
                                    &times;
                                </span>
                                <span class="sr-only">Close</span>
                            </button>
                            @Html.ValidationSummary(false)
                        </div>
                    }
                </div>
            </div>
            <!-- END MESSAGE AREA -->
            <div class="form-group">
                @Html.LabelFor(m => m.Entity.FirstName)
                @Html.TextBoxFor(m => m.Entity.FirstName, new { @class = "form-control" })
                @Html.LabelFor(m => m.Entity.LastName)
                @Html.TextBoxFor(m => m.Entity.LastName, new { @class = "form-control" })
                @Html.LabelFor(m => m.Entity.Title)
                @Html.TextBoxFor(m => m.Entity.Title, new { @class = "form-control" })
                @Html.LabelFor(m => m.Entity.Email)
                @Html.TextBoxFor(m => m.Entity.Email, new { @class = "form-control" })
                @Html.LabelFor(m => m.Entity.Phone)
                @Html.TextBoxFor(m => m.Entity.Phone, new { @class = "form-control" })
                @Html.LabelFor(m => m.Entity.PhoneExtension)
                @Html.TextBoxFor(m => m.Entity.PhoneExtension, new { @class = "form-control" })
                @Html.LabelFor(m => m.Entity.IsActive)
                @Html.EditorFor(m => m.Entity.IsActive)
                @Html.LabelFor(m => m.Entity.AccessLevelID)
                @Html.TextBoxFor(m => m.Entity.AccessLevelID, new { @class = "form-control" })
            </div>
        </div>
        <div class="panel-footer">
            <button id="btnSave" class="btn btn-sm btn-primary" data-crm-action="save" formnovalidate="formnovalidate">
                <i class="glyphicon glyphicon-floppy-disk"></i>&nbsp;Save
            </button>
            <button id="btnCancelAdd" class="btn btn-sm btn-primary" data-crm-action="cancelAdd" formnovalidate="formnovalidate">
                <i class="glyphicon glyphicon-remove-sign"></i>&nbsp;Cancel
            </button>
        </div>
    </div>
}

I use the same request management pattern and have two validation summary fields (one is within modal window that works perfectly though I do not see why the same odd behaviour is not shown) that I need to suppress validation before submit. 我使用相同的请求管理模式,并且有两个验证摘要字段(一个模态窗口中的一个窗口很正常,尽管我看不到为什么未显示相同的奇怪行为),我需要在提交之前取消验证。

I have tried setting, 我尝试设置

formnovalidate="formnovalidate"

tag for the buttons I hope to suppress validation and this works with only an annoying side effect that is mentioned here. 我希望抑制按钮的按钮标记,这只能解决这里提到的烦人的副作用。

What worked for me is, on js side setting the e.preventdefault as an option for only the request I want to validate like below: 对我有用的是,在js端,将e.preventdefault设置为仅用于我要验证的请求的选项,如下所示:

command = $(this).data("gtb-action");
if (command === "PostNoteEntity") {
    e.preventDefault();
}

Please, do try! 请尝试!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM