【问题标题】:Form submit inside Ajax JQuery在 Ajax JQuery 中提交表单
【发布时间】:2017-03-24 06:49:54
【问题描述】:

我必须在 Ajax 中提交一个表单。 我收到“form.submit 不是函数”的 JQuery 错误。

$("#form").validate({

    // Specify the validation rules
    rules: {
        type: "required",
        groups: {
            required: true
        }
    },

    // Specify the validation error messages
    messages: {
        type: "Type is required",
        groups: {
            required: "Group is required" 
        }
    },

    submitHandler: function(form) {
        $.ajax({
            type: "POST",
            url: "check_exists.php",
            data: {
                  groups: $( "#groups" ).val(),
                  type: $( "#type" ).val()
            },
            success: function(data) {
              if(data == "true") {
                    form.submit();  // It shows form.submit is not a function
              } else {
                    // Displays error
              }

            }
        });        
    }
});

当我在 Ajax 上方给出 form.submit() 函数时,它起作用了! 那么如何在 Ajax 成功函数中提交这个表单呢?

【问题讨论】:

    标签: javascript jquery ajax forms


    【解决方案1】:

    嘿,你可以在这里使用箭头功能功能

    $("#form").validate({    
        // Specify the validation rules
        rules: {
            type: "required",
            groups: {
                required: true
            }
        },
    
        // Specify the validation error messages
        messages: {
            type: "Type is required",
            groups: {
                required: "Group is required" 
            }
        },
    
        submitHandler: function(form) {
            $.ajax({
                type: "POST",
                url: "check_exists.php",
                data: {
                      groups: $( "#groups" ).val(),
                      type: $( "#type" ).val()
                },
                success: (data) => {
                  if(data == "true") {
                        form.submit();
                  } else {
                        // Displays error
                  }
    
                }
            });        
        }
    });
    

    它将回调函数的上下文绑定到 submitHandler 函数

    【讨论】:

    • 对不起,不工作!我收到相同的“form.submit 不是函数”JQuery 错误
    • console.log(form) in submitHandler .. 它说什么?
    【解决方案2】:

    尝试更改form 变量的范围:

    var form_to_be_submitted = null;
    $("#form").validate({    
        // Specify the validation rules
        rules: {
            type: "required",
            groups: {
                required: true
            }
        },
    
        // Specify the validation error messages
        messages: {
            type: "Type is required",
            groups: {
                required: "Group is required" 
            }
        },
    
        submitHandler: function(form) {
            form_to_be_submitted = form;
            $.ajax({
                type: "POST",
                url: "check_exists.php",
                data: {
                      groups: $( "#groups" ).val(),
                      type: $( "#type" ).val()
                },
                success: function(data) {
                  if(data == "true") {
                        form_to_be_submitted.submit();  // It shows form.submit is not a function
                  } else {
                        // Displays error
                  }
    
                }
            });        
        }
    });
    

    【讨论】:

    • 对不起,不工作!我收到相同的 'form_to_be_submitted.submit is not a function' JQuery 错误
    • 你检查form_to_be_submitted的值了吗?它输出什么?
    • 我已经检查了 ajax 和 ajax 之上的 form_to_be_submitted 值。两个警报相同的值
    猜你喜欢
    • 2021-09-30
    • 2011-04-23
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多