首页 > 解决方案 > 如果我在验证电子邮件时收到错误,请重新加载表单

问题描述

我在 .html 文件中有这样的表格:

 <body>
 <script>
      $(document).ready(function(){
         $("#submit").click(function(){
           if($("#mail").val() != $("#rmail").val()){
              alert("emails don't match");
              //?
           }
         });
      });
 </script>


<form action="action.php" method="POST">         
  <p>E-mail:</p>
    <input type = "email" id="mail" name = "correo">
  <p>Repetir e-mail:</p>
    <input type = "email" id="rmail" name = "rcorreo">
    <input id="submit" type="submit"/>
</form>
</body>

如果邮件不匹配,J-Query 函数会显示警报,然后转到 action.php 页面

如果邮件不匹配,我想要做的是重新加载表单并防止进入 action.php 页面。我试过评论location.reload();在哪里,//?但它不起作用。

有什么线索吗?提前致谢。

标签: jqueryhtmlforms

解决方案


<body>
 <script>
      $(document).ready(function(){
         $("#submit").click(function(event){
           if($("#mail").val() != $("#rmail").val()){
              event.preventDefault();
              alert("emails don't match");
              window.location.reload();
           }
         });
      });
 </script>

我希望这会有所帮助


推荐阅读