Form not submitting inside $.ajax success function

前端 未结 11 1574
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 20:34

I\'m validating for duplicate names by using jquery+Ajax. Everything is working fine except that the form is not submitting once everything returns true

What

11条回答
  •  孤城傲影
    2021-01-18 21:22

    We had the same problem and think we sorted out.

    The thing is that the .submit() action doesn't work inside the "thread" $.ajax neither $.post. For that you have to add the async:false instruction inside the $.ajax block and submit the form when the $.ajax execution finishes.

    Not tested but this is the working idea:

    html:

    
    

    javacript:

    function Mycheck() {
    
       var result = "";
       $.ajax {
           async:false,
           url: you_url_here,
           timeout: 15000,
           success: function(data) {
                result="success";
           },
           error: function(request, status, err) {
                result="error";
           }
       };
    
       // Here, if success -> SUBMIT FORM or whatever
       // Only get here if "ASYNC" = FALSE!!!
       if (result == "success") {
           $("form").submit(); // this submits the form
           return;
       } else {
           alert('error');
           // the form will not be post
       }
    }
    

提交回复
热议问题