简体   繁体   English

表单多次单击按钮提交多个时间

[英]Form Submit Multiple Time On Multiple Click Of Button

I am trying to create exception by submiting form using AJAX and exception is also created when button 我试图通过使用AJAX提交表单来创建异常,并且在按钮时也会创建异常

<button type="button" onclick="submitForm()" class="btn btn-s-md btn-primary saveButton mb-10 "><i class="fa fa-save fa-fw"></i>Save</button>

is clicked and onclick the function function submitForm() is executed. 单击并onclick后,将执行函数function submitForm()

The problem is that on multiple click of button the form is submitted as well which i don't want. 问题是,在按钮的多次单击上,表单也被提交了,我不想这样做。

Below is my form 下面是我的表格

<form asp-action="CreateException" asp-controller="Attorney" method="post" role="form" enctype="multipart/form-data" id="attorneyExceptionForm">
        <div class="panel panel-default">
            <div class="panel-body">
                <div class="row">
                    <div class="col-md-3 col-lg-3 col-sm-12">
                        <div class="form-group">
                            <label asp-for="Description" class="control-label ">Description<span class="requAstrik">*</span></label>
                            <input asp-for="Description" class="form-control " />
                            <span asp-validation-for="Description" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="col-md-3 col-lg-3 col-sm-12">
                        <div class="form-group">
                            <label asp-for="Exception" class="control-label ">Exception<span class="requAstrik">*</span></label>
                            <select id="slctExceptionA" class="form-control select2" asp-for="Exception" style="color: #444 !important;width:100%;" asp-items="@(new SelectList(@ViewBag.ExcsList, "ExcId", "ExcDescription" ))"></select>

                            <span id="attorneyExceptionError" asp-validation-for="Exception" class="text-danger"></span>
                        </div>
                    </div>                       
                </div>                   
            </div>
        </div>           
        <div class="form-group">
            <button type="button" onclick="submitForm()" class="btn btn-s-md btn-primary saveButton mb-10 "><i class="fa fa-save fa-fw"></i>Save</button>
        </div>
    </form>

Below is my js function 下面是我的js函数

<script>
function submitForm() {
    debugger;
    var datastring = $("#attorneyExceptionForm").serialize();
    var validForm = $("#attorneyExceptionForm").valid();
    if (!validForm) { return false; }
    $.ajax({
        type: "POST",
        url: "/Attorney/CreateAttorneyException",
        data: datastring,
        dataType: "json", 
        success: function (data) {
            debugger;
            if (data.success.Result) {
                debugger;
                $("#myModal5").modal('hide');
                var grid = $("#AttorneyExceptionGrid").dxDataGrid('instance');
                grid.refresh();
                showNotification("Exception saved successfully", "success");
            }
            else {
                debugger;
                showNotification(data.success.MessageBody + " is required ", "warning");
            }
        },
        error: function (data) {
            debugger;
            showNotification("Please input the required field", "warning");
        }
    });
}
</script>

You can disable the button in the first click and enable it again when the ajax response arrive. 您可以在第一次单击中禁用该按钮,然后在ajax响应到达时再次启用它。

<button type="button" onclick="submitForm(this)" class="btn btn-s-md btn-primary saveButton mb-10 "><i class="fa fa-save fa-fw"></i>Save</button>

function submitForm(e) {
   $(e).prop("disabled", true);
  //more code
}

And enable it again in the success or error ajax method 并在成功或错误ajax方法中再次启用它

 $(e).prop("disabled", false);

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

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