gpt4 book ai didi

jquery 表单发布了两次

转载 作者:行者123 更新时间:2023-12-01 00:37:18 25 4
gpt4 key购买 nike

我在按钮单击中使用以下 JavaScript 代码来发布我的表单。

var formIsValid = $('#createFreezerForm').valid();
if (formIsValid) {
$('#createFreezerForm').submit();
}
return false;

我正在为使用 MVC3 Ajax.Form 帮助程序创建的表单执行此操作。

不知何故,我的表单被发布了两次。有什么建议吗?

这是生成的html

<div id="freezerDialog" class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 64px; height: auto; "><form action="/BiobankWebsite/Storage/Freezer/Create" data-ajax="true" data-ajax-failure="errorAjaxCall" data-ajax-method="post" data-ajax-mode="replace" data-ajax-success="freezerOverview.formPostComplete" data-ajax-update="#createFreezerForm" id="createFreezerForm" method="post">
<input name="__RequestVerificationToken" type="hidden" value="cu92co3Mwzt28mB3WCXGsmKYJ4RUQJxPcUUtQ4jyOcjrg82Y0QRJtGcmP818Isbd6bYqqMXj9xJOzt18TkSzFQerWNnu4F6b8pjSvvWjXNRSC3LYvisDR9+jjkG0ygtBWNSowIvdMva+Cq/9X9B9cQT2x6yOauFRen7vkmVDQMDJu9LWMZH4Z3Q6IEbonUMk"><input data-val="true" data-val-required="The FreezerId field is required." id="FreezerId" name="FreezerId" type="hidden" value="00000000-0000-0000-0000-000000000000">
<div class="validation-summary-valid" data-valmsg-summary="true">
<span>Validation messages</span>
<ul><li style="display:none"></li></ul>
</div>
<div class="left marginRight">
<div class="editor-label">
<label for="FreezerType">Freezer type</label>
</div>
<div class="editor-field">
<select data-val="true" data-val-required="The Freezer type field is required." id="FreezerType" name="FreezerType">
<option value="">Select one...</option>
<option value="97cecf57-e596-4c6a-a43f-0eaed4e6a560">K10</option>
<option value="e7a05273-1d4d-42ad-83a4-3e19f1b48e64">K38</option>
<option value="304ed3e3-75c7-4437-a71b-5e26c718b684">U725</option>
<option value="31bc9e43-c4da-4e84-a86e-62a25b122d56">U45</option>
<option value="890256e8-036f-4a03-946a-fdc069b4b2e1">LBH</option>
</select>
<span class="field-validation-valid" data-valmsg-for="FreezerType" data-valmsg-replace="false">*</span>
</div>
<div class="editor-label">
<label for="FreezerNameFormat">Freezer name format</label>
</div>
<div class="editor-field">
<input class="medium" id="FreezerNameFormat" name="FreezerNameFormat" type="text" value="">
<span class="field-validation-valid" data-valmsg-for="FreezerNameFormat" data-valmsg-replace="false">*</span>
<span id="freezerName" class="bold"></span>
</div>
</div>
<div class="left">
<div class="editor-label">
<label for="Temperature">Temperature</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-number="The field Temperature must be a number." data-val-required="The Temperature field is required." id="Temperature" name="Temperature" type="text" value="0">
<span class="field-validation-valid" data-valmsg-for="Temperature" data-valmsg-replace="false">*</span>
</div>
<div class="editor-label">
<label for="FreezerNr">Freezer number</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-number="The field Freezer number must be a number." data-val-required="The Freezer number field is required." id="FreezerNr" name="FreezerNr" type="text" value="0">
<span class="field-validation-valid" data-valmsg-for="FreezerNr" data-valmsg-replace="false">*</span>
</div>
</div>
<div class="clear">
<div class="editor-label">
<label for="Location">Location</label>
</div>
<div class="editor-field">
<input class="text-box single-line" id="Location" name="Location" type="text" value="">
<span class="field-validation-valid" data-valmsg-for="Location" data-valmsg-replace="false">*</span>
</div>
</div>
<div class="clear marginTop">
<button type="submit" style="display: none; ">
Create</button>
</div>
</form></div>

这个 html 通过 jquery get 放入对话框 div 中。正如您所看到的,我从我的部分中删除了原始的提交按钮,并向我的对话框添加了一个新按钮。

负责这个魔法的 JavaScript 函数:

var showCreateDialog = function () {
getForm(urls.freezer.create, '#createFreezerForm');
dialog.dialog({
title: 'Create freezer',
width: 400,
resizable: false,
buttons: {
Create: function (event) {
event.preventDefault();
var formIsValid = $('#createFreezerForm').valid();
if (formIsValid) {
$('#createFreezerForm').submit();
}
return false;
},
Cancel: function () {
$(this).dialog("close");
}
}
});
dialog.dialog('open');
};

var getForm = function (url, formId) {
$.get(url, null, function (html) {
dialog.html(html);
hideAndDisableOriginalButton(formId);
$.validator.unobtrusive.parse("#createFreezerForm");
}, 'html');
};

var formPostComplete = function (response) {
dialog.dialog('close');
grid.trigger('reloadGrid');
};

var hideAndDisableOriginalButton = function (formId) {
var freezerFormCreateButton = $(formId + ' button:submit');
freezerFormCreateButton.attr("disabled", "true");
freezerFormCreateButton.hide();
};

最佳答案

我发现了这个问题。不知何故,我在该特定页面上包含了两次脚本。这就是为什么所有内容都会执行两次。

以下脚本包含两次:

<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>

关于jquery 表单发布了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7258247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com