javascript - 表单提交两次
问题描述
我有一个 asp.net MVC 应用程序。
下面的 javascript 代码自 10 天前开始投入生产。这个想法是在允许提交表单之前拦截提交事件并调用 ajax 事件来检查附件大小。
问题:我们报告了 2 个用户提交了两次表单。我不知道这怎么可能。也许使用较旧的 IE 版本?
$(function () {
$("input[type=submit]").bind("click", function (e) {
e.preventDefault();
e.returnValue = false;
var $form = $(this);
$.ajax({
type: 'post',
url: pMailPreview.checkAttachmentsSizeExceedUrl,
cache: false,
context: $form,
success: function (result, textStatus, XMLHttpRequest) {
if (result.sizeExceed) {
alert('Size exceed !!');
} else {
// make sure that you are no longer handling the submit event; clear handler
this.off('submit');
// actually submit the form
this.submit();
}
},
error: function (jqXhr, textStatus, errorThrown) {
alert("Error '" + jqXhr.status + "' (textStatus: '" + textStatus + "', errorThrown: '" + errorThrown + "')");
},
});
return false;
});
});
解决方案
如果用户在按钮上单击两次并且表单有效,它将被发送两次。
您可以禁用提交按钮。
推荐阅读
- c# - Nmodbus 返回 NotImplementedException: Function code 123 not supported exception while reading response data
- java - 使用 Spring Boot 在 TestNg 测试用例中提供 BeanFactory Bean
- guile - 配置说没有找到 guile-config
- django - 如何进行查询以获取 Django ORM 中每个组的最后一个元素?
- python - 在 Python 中对来自不同类别的 n 长度数组的分类数据进行编码
- heroku - 在 Heroku 上使用 Redis 的服务器端 Flask 会话
- jquery - 如何使用输入文本和选择对数据表应用高级搜索?
- html - CSS:对齐表单选项
- java - 当Api返回响应时,如何以角度显示“登录失败。请重试”消息?
- spring - 将非英文字符存储到mysql数据库