我必须在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成功函数中提交此表单?

最佳答案

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

$("#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函数

09-25 16:46