我有一个通过jQuery捕获并通过AJAX发送的表单。我的问题是,每次刷新页面时,表单都提交一次以上。
我试图取消提交按钮,但然后表单通常在第一次提交后发布。如能提供任何帮助,将不胜感激。
$('#exportForm').submit(function() {
$.ajax({
type: "POST",
url: $(this).attr('action'),
data: $(this).serialize(),
success: function(response) {
$('#exportForm').unbind('submit');
console.log(response);
}
});
return false;
});发布于 2013-11-25 14:29:42
很可能是使用button或submit来触发ajax事件。试试这个:
$('#exportForm').submit(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: $(this).attr( 'action' ),
data: $(this).serialize(),
success: function( response ) {
console.log( response );
}
});
return false;
});发布于 2014-10-08 16:36:21
除了调用preventDefault之外,还在事件中调用stopImmediatePropagation。
$('#exportForm').submit(function(e){
e.preventDefault();
e.stopImmediatePropagation();
$.ajax({
type: "POST",
url: $(this).attr( 'action' ),
data: $(this).serialize(),
success: function( response ) {
console.log( response );
}
});
return false;
});发布于 2015-05-03 02:17:52
如果您使用某种验证(例如jQuery验证),表单会提交两次,因为除了您自己编写的$('#exportForm').submit之外,验证插件在成功验证所有字段之后也会提交表单。
注意:如果您正在使用jQuery验证,请避免使用.submit()。相反,使用submitHandler。
https://stackoverflow.com/questions/20195483
复制相似问题