我在按钮中使用以下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();
};
答案 0 :(得分:14)
我发现了这个问题。不知何故,我在该特定页面上两次包含了脚本。这就是为什么一切都被执行两次。
以下脚本包括两次:
<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>
答案 1 :(得分:0)
你错过了return
吗?例如onsubmit="return your_function();"
答案 2 :(得分:0)
我怀疑你原来的按钮也在提交表格。要防止这种情况,请将代码更改为:
function somethingClicked(event) { event.preventDefault(); var formIsValid = $('#createFreezerForm').valid(); if (formIsValid) { $('#createFreezerForm').submit(); } return false; }
以下是event.preventDefault()的参考资料。你需要发布更多的代码,因为我们现在都在黑暗中。
答案 3 :(得分:0)
由于jquery.unobtrusive-ajax.js
也将事件附加到提交按钮,因此会发生两个提交。
您无需将代码附加到提交按钮单击事件,因为它会复制已附加到该事件的jquery.unobtrusive-ajax.js代码。删除您的代码,它将按预期工作。