简体   繁体   English

js验证仅运行一次

[英]js validation only runs once

I'm having an issue with the code below where if I put an invalid email address in it will properly show the signUpWarning alert the first time but if I close that box and try the same invalid email it won't pop back up. 我的下面的代码存在问题,如果我在其中输入无效的电子邮件地址,它将在第一次正确显示signUpWarning警报,但是如果我关闭该框并尝试使用相同的无效电子邮件,它将不会弹出。 Any ideas? 有任何想法吗?

    $(document).ready(function() {

    //if submit button is clicked
    $('#submit_btn').click(function(e) {
        e.preventDefault();
        var email = $('#email').val();
        if (!is_valid_email(email)){
            $("#signUpWarning").show();
        }else{

            $.ajax({
                url: "process.php",
                type: "post",
                data: "email=" + email,
                success: function() {
                    $("#signUpWarning").hide();
                    $("#signUpAlert").show();
                }
            });
        }
    });
});

function is_valid_email (email)
{
    return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}

The code for the alert box is: 警报框的代码是:

  <div class="alert alert-block alert-error hide" id="signUpWarning"><!--Use: $("#signUpWarning").show(); to show this alert-->
    <a class="close" data-dismiss="alert">×</a>
    <h5 class="alert-heading">Sorry!</h5>&nbsp;Your e-mail address was not valid, please try again.
  </div>

Check that you're not removing the element from the DOM when closing the alert. 关闭警报时,请检查是否没有从DOM中删除元素。

console.log($("#signUpWarning")); before the $("#signUpWarning").show(); $("#signUpWarning").show(); should help determine if that's what's happening. 应该有助于确定是否正在发生这种情况。

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

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