为什么我的jQuery Ajax表单提交一次,第一次提出,在第二次提交...?

编程入门 行业动态 更新时间:2024-10-26 23:29:05
本文介绍了为什么我的jQuery Ajax表单提交一次,第一次提出,在第二次提交...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个简单的AJAX形式的作品,因为它应该当我提交。不过,如果我再输入新数据相同的形式(无需刷新页面),然后将其提交表单的两倍。如果我这样做的第三时间,然后将其提交表单三个时间,依此类推。为什么这样做呢?这是我的code:

$(文件)。就绪(函数(){     $(#myForm会)。递交(函数(){         VAR MyField的= $('#身份识别码).VAL();         $阿贾克斯({             键入:POST,             网址:myFile.php,             数据类型:HTML,             数据:{myData的:MyField的},             成功:功能(数据){                 警报(数据);             }         });         返回false;     }); });

解决方案

即使在开发AJAX登录表单我被困同样的问题。在使用Google的几个小时后,我找到了解决办法。我希望这会帮助你的。

基本上你要的取消绑定后表单的提交操作的当前实例的Ajax请求被完成。 所以在返回false加入这一行;

$(#的MyForm)解除绑定(提交)。

所以整个code应该像

    $(文件)。就绪(函数(){         $(#myForm会)。递交(函数(){             VAR MyField的= $('#身份识别码).VAL();             $阿贾克斯({                 键入:POST,                 网址:myFile.php,                 数据类型:HTML,                 数据:{myData的:MyField的},                 成功:功能(数据){                     警报(数据);                 }             });             / *加入这一行* /             $(#的MyForm)解除(提交);             返回false;         });     });

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:

$(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; }); });

解决方案

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.

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;

$("#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; }); });

更多推荐

为什么我的jQuery Ajax表单提交一次,第一次提出,在第二次提交...?

本文发布于:2023-11-23 04:29:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620098.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   jQuery   Ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!