表单在Ajax JQuery中提交

时间:2017-03-24 06:49:55

标签: javascript jquery ajax forms

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

2 个答案:

答案 0 :(得分:0)

尝试更改<div class="container"> <div class="radio-grp"> <input type="radio" name="demo" value="1" id="radio1" checked> <label for="radio1"></label> <label for="radio1" class="radio-txt">Demo 1</label> </div> <div class="radio-grp"> <input type="radio" name="demo" id="radio2" value="2"> <label for="radio2"></label> <label for="radio2" class="radio-txt">Demo 2</label> </div> <div class="radio-grp"> <input type="radio" name="demo" id="radio3" value="3"> <label for="radio3"></label> <label for="radio3" class="radio-txt">Demo 3</label> </div> </div>变量的范围:

form

答案 1 :(得分:0)

嘿,你可以使用此处的箭头功能

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