为什么我的jQuery Ajax表单提交一次,第一次提出,在第二次提交...? [英] Why does my jquery ajax form submit once on first submit, twice on second submit...?

查看:175
本文介绍了为什么我的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表单提交一次,第一次提出,在第二次提交...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆