I have a simple AJAX form that works as it should when I submit it. However, if I then enter new data into the same form (without refreshing the page), then it submits the form twice. If I do this a third time, then it submits the form three time, and so on. Why is it doing this? Here is my code:
我有一个简单的AJAX表单,它在我提交时可以正常工作。但是,如果我然后将新数据输入到相同的表单中(不刷新页面),则它会提交表单两次。如果我第三次这样做,那么它会三次提交表单,依此类推。它为什么这样做?这是我的代码:
$(document).ready(function(){
$("#myForm").submit(function() {
var myField = $('#myID).val();
$.ajax({
type: "POST",
url: 'myFile.php',
dataType: 'html',
data: {myData:myField},
success: function(data){
alert(data);
}
});
return false;
});
});
3 个解决方案
#1
9
Even I got stuck with the same problem while developing a AJAX login form. After an googling for couple of hours I found a solution. I hope this should help you out.
甚至在开发AJAX登录表单时我也遇到了同样的问题。经过几个小时的谷歌搜索后,我找到了解决方案。我希望这能帮到你。
Basically you have to unbind the current instance of form's submit action after your ajax request gets completed. So add this line before return false;
基本上,在ajax请求完成后,您必须取消绑定表单提交操作的当前实例。所以在返回false之前添加这一行;
$("#myform").unbind('submit');
Therefore your entire code should look like
因此,您的整个代码应该是这样的
$(document).ready(function(){
$("#myForm").submit(function() {
var myField = $('#myID).val();
$.ajax({
type: "POST",
url: 'myFile.php',
dataType: 'html',
data: {myData:myField},
success: function(data){
alert(data);
}
});
/* Add this line */
$("#myform").unbind('submit');
return false;
});
});
#2
1
I had the same problem.
我有同样的问题。
I found the solution in: jquery : submitting a form twice
我找到了解决方案:jquery:提交表单两次
Basicaly is adding .submit() to Saurabh Mishra answer:
Basicaly正在向Saurabh Mishra添加.submit()回答:
$('#form1').unbind('submit').submit();
#3
0
Unbinding the form can break you Ajax from. I found that as long as I declare the form first.
解除绑定表单可能会破坏您的Ajax。我发现只要我先申报表格。
$('#my_form').ajaxForm
that all works as expected, and follows the standard protocol. I just spent a day chasing the 'missing template' error on the form submission because I unbound the form.
这一切都按预期工作,并遵循标准协议。我只花了一天时间在表单提交上追逐“缺少模板”错误,因为我解除了表单的限制。
#1
9
Even I got stuck with the same problem while developing a AJAX login form. After an googling for couple of hours I found a solution. I hope this should help you out.
甚至在开发AJAX登录表单时我也遇到了同样的问题。经过几个小时的谷歌搜索后,我找到了解决方案。我希望这能帮到你。
Basically you have to unbind the current instance of form's submit action after your ajax request gets completed. So add this line before return false;
基本上,在ajax请求完成后,您必须取消绑定表单提交操作的当前实例。所以在返回false之前添加这一行;
$("#myform").unbind('submit');
Therefore your entire code should look like
因此,您的整个代码应该是这样的
$(document).ready(function(){
$("#myForm").submit(function() {
var myField = $('#myID).val();
$.ajax({
type: "POST",
url: 'myFile.php',
dataType: 'html',
data: {myData:myField},
success: function(data){
alert(data);
}
});
/* Add this line */
$("#myform").unbind('submit');
return false;
});
});
#2
1
I had the same problem.
我有同样的问题。
I found the solution in: jquery : submitting a form twice
我找到了解决方案:jquery:提交表单两次
Basicaly is adding .submit() to Saurabh Mishra answer:
Basicaly正在向Saurabh Mishra添加.submit()回答:
$('#form1').unbind('submit').submit();
#3
0
Unbinding the form can break you Ajax from. I found that as long as I declare the form first.
解除绑定表单可能会破坏您的Ajax。我发现只要我先申报表格。
$('#my_form').ajaxForm
that all works as expected, and follows the standard protocol. I just spent a day chasing the 'missing template' error on the form submission because I unbound the form.
这一切都按预期工作,并遵循标准协议。我只花了一天时间在表单提交上追逐“缺少模板”错误,因为我解除了表单的限制。