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

2023-09-10 16:56:56 作者:矫情什么

我有一个简单的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;
        });
    });


jq ajax提交合并表单,JQuery通过Ajax提交表单并返回结果

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