简体   繁体   English

点击后提交表格

[英]Submit form after click

Having Following form validation through jQuery: 通过jQuery进行了以下表单验证:

$("#Login").submit(function( event ) {
    var url = 'ajax/';
    var data = {};
    $("input").each(function() {
        data[$(this).attr('name')] = $(this).val(); 
    });
    $.post(url,data,function( resp ) {
        $("#formLoginErrorMessage").children().remove();
        if(resp === " ")
        {                
            console.log("Empty");
            return; // The form should submit
        } else if (typeof resp === "object") {
            console.log(resp);
        } else{
            $("#formLoginErrorMessage").addClass("alert-danger");
            $("#formLoginErrorMessage").append("<li>" + resp + "</li>");
            console.log(resp);
        }
    },'json');
    event.preventDefault();
});

This script is checking some errors and when resp is empty form should submit. 该脚本正在检查一些错误,并且当resp为空时,应提交表单。 But return from if where resp is checked doesn't seem to make form submit. 但是,如果从那里RESP检查似乎并没有使表单提交回来

You can call the submit() method in the if condition 您可以在if条件中调用Submit()方法

$("#Login").submit(function (event) {
    var form = this;
    var url = 'ajax/';
    var data = {};
    $('input').each(function () {
        data[$(this).attr('name')] = $(this).val();
    });
    $.post(url, data, function (resp) {
        $("#formLoginErrorMessage").children().remove();
        if (resp === " ") {
            form.submit();
            console.log("Empty");
            return; // The form should submit
        } else if (typeof resp === "object") {
            console.log(resp);
        } else {
            $("#formLoginErrorMessage").addClass("alert-danger");
            $("#formLoginErrorMessage").append("<li>" + resp + "</li>");
            console.log(resp);
        }
    }, 'json');
    event.preventDefault();
});

What you seem to be confused about are closures 您似乎很困惑的是闭包

$( "#Login" ).submit(function( event ) {

   // main submit handling function starts here

   ...

   $.post(url,data,function(resp){

       // inner response handling function starts here

        if(resp === " "){               
               console.log("Empty");
               return; //This returns to jQuery internals somewhere inside $.post
        } ...
    },'json');

    // stops form from submitting
    event.preventDefault();
});

I added a couple of comments to help you get a better visual of the scope. 我添加了一些评论,以帮助您更好地了解范围。

First thing first, you should never rely on client-side validation. 首先,您不应该依赖客户端验证。 It can be overtaken without exceptions. 可以毫无例外地超越它。 If for some reason you insist on having it this way you can use some outer scope variable to determine whether validation has passed or not, but then you have to use $form.submit() instead of return to actually submit the form. 如果由于某种原因坚持使用这种方法,则可以使用一些外部作用域变量来确定验证是否通过,但是您必须使用$form.submit()而不是return来实际提交表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM