dousong2967 2016-05-24 14:29
浏览 42
已采纳

如果在ajax回调中验证失败,我如何停止提交表单

i am trying to submit a form after validation using ajax. after ajax return if input field is valid then form should submit otherwise error message. below is my html

<form action="new-join.php" method="post" enctype="multipart/form-data" onSubmit="return CheckForm()" id="NewJoinsub">

<!-- my form -->

<button type="submit" class="button btn btn-primary btn-large">Continue</button>
</form>

and ajax is

<script>
function CheckForm(e) {
    var checkpin=$("#JoiningPin").val();

     $.post("check-pin.php", { checkpin: checkpin }, 
     function(result){  
                //if the result is 1 submit  
                if(result == 1){  
                $('#NewJoinsub').submit()
              }else{  
                   var response_not = "<div style='color: #f30;'>Not A Valid Pin or Already Used</div>";
                    $('#pinCheck').html(response_not);  
                    e.preventDefault();
                }  
        });  
}(jQuery);
</script>

my form is validating correctly but its not preventing form submission if condition is failed. where i am doing wrong ?

  • 写回答

3条回答 默认 最新

  • dongsigan2636 2016-05-24 14:50
    关注

    Checkout this:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form action="new-join.php" method="post" enctype="multipart/form-data" id="NewJoinsub">
      <!-- my form -->
      <button type="submit" class="button btn btn-primary btn-large">Continue</button>
    </form>
    
    <script>
    $('#NewJoinsub').on('submit',function(e) {
        e.preventDefault();
        var checkpin=$("#JoiningPin").val();
    
        $.post("check-pin.php", { checkpin: checkpin }, function(result){  
            //if the result is 1 submit  
            if(result == 1){  
                $(this).submit();
            }
        else {  
                var response_not = "<div style='color: #f30;'>Not A Valid Pin or Already Used</div>";
          $('#pinCheck').html(response_not);  
        }  
      });  
    });
    </script>

    You have first to e.preventDefault(); !

    To complete your example:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form action="new-join.php" method="post" enctype="multipart/form-data" id="NewJoinsub" onsubmit="checkForm(event);">
      <!-- my form -->
      <button type="submit" class="button btn btn-primary btn-large">Continue</button>
    </form>
    
    <script>
      function checkForm(e) {
          e.preventDefault();
          var checkpin=$("#JoiningPin").val();
    
        $.post("check-pin.php", { checkpin: checkpin }, function(result){  
          //if the result is 1 submit  
          if(result == 1){  
            $(this).submit();
          }
          else {  
            var response_not = "<div style='color: #f30;'>Not A Valid Pin or Already Used</div>";
            $('#pinCheck').html(response_not);  
          }  
        });  
      }(jQuery);
    </script>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog