MVC - 用户双击“提交”按钮时出现问题
MVC - Issue with users double-clicking Submit button
我的MVC应用程序中有许多页面,用户单击"提交"按钮即可发布表单。有时用户会单击"提交",由于不会立即执行任何操作,请再次单击它。因此,表格提交两次。为了防止这种情况,我有以下JavaScript代码:
// When the user submits the form, disable the Save button so there is no chance
// the form can get double posted.
$('#form').submit(function () {
$(this).find('input[type=submit]').prop('disabled', true);
return true;
});
此代码禁用"提交"按钮,以便用户无法单击两次。这工作正常。但是,如果表单上存在客户端验证错误,则会禁用"提交"按钮,但永远不会发布表单,现在用户无法发布表单。我可以对 JS 代码进行更改以检测是否存在客户端验证错误,如果是,我要么不禁用"提交"按钮,要么重新启用它?
如果您使用的是 jQuery Validate,则可以在禁用按钮之前检查表单是否有效:
$('#form').submit(function () {
if ($(this).valid()) {
$(this).find('input[type=submit]').prop('disabled', true);
}
});
你可以尝试这样的事情:
<button id="subButton" /> <!-- do not use type="submit" because some browsers will automatically send the form -->
Javascript:
$('#subButton').click(function (e) {
e.preventDefault(); //prevent browser's default behaviour to submit the form
$(this).prop('disabled', true);
doValidation();
});
var pTimeout;
function doValidation() {
ajaxLoader.show(); //lock the screen with ajaxLoader
var form = $('#registerForm');
var isPending = form.validate().pendingRequest !== 0; // find out if there are any pending remote requests ([Remote] attribute on model)
if (isPending) {
if (typeof pTimeout !== "undefined") {
clearTimeout(pTimeout);
}
pTimeout = setTimeout(doValidation, 200); //do the validation again until there are no pending validation requests
}
var isValid = form.valid(); //have to validate the form itself (because form.Valid() won't work on [Remote] attributes, thats why we need the code above
if (!isPending) {
ajaxLoader.hide();
if (isValid) {
$('#registerForm').submit(); //if there are no pending changes and the form is valid, you can send it
}
else {
$('#subButton').prop('disabled', false); //else we reenable the submit button
}
}};
切换它。
$("input[type='submit']").on("click", function (event) {
event.preventDefault();
$(this).prop("disabled", true);
// perform error checking
if (noErrors) {
$("#form").submit();
}
else {
$(this).prop("disabled", false);
}
});
相关文章:
- 后退按钮问题
- Javascript上传按钮问题,适用于Wordpress
- 点击jquery上的按钮问题
- Safari浏览器返回按钮问题
- 表单操作过程中出现后退按钮问题
- 单选按钮切换按钮问题
- JavaScript/jQuery .click() 按钮问题
- 页面哈希和后退按钮问题 phonegap+Jquery
- 角度 UI 路由器后退按钮问题
- 将多个事件附加到 IE 中的按钮问题
- css 按钮宽度作为百分比?响应按钮问题
- JQuery 未检查单选按钮问题
- JavaScript 单选按钮问题
- 重置按钮问题
- Ember.js路由和后退按钮问题
- jquerymobile动态更改按钮问题的文本
- AngularJS中的Skype URI按钮问题
- 使用 jquery 多次点击按钮问题
- Windows Phone PhoneGap应用返回按钮问题
- 使用jquery和php提交按钮问题