来自输入字段的值未显示在alert()中

时间:2018-05-05 18:16:34

标签: javascript html validation

我的表格如下。我想我已经正确地从输入文本字段中获取了值。但是当我alert()它时,它什么也没有显示出来。 (甚至不是空的)

以下表格:



var firstName = document.getElementById('firstName').value;
          var lastName = document.getElementById('lastName').value;
          var email = document.getElementById('email').value;



          $("#validationAlert").hide();
          $("#btn-sign-up").click(function showAlert() {
              if (firstName == "") {
                  $("#firstName").focus();

                  alert(firstName); //this shows nothing even when I type something

                  $("#validationAlert").fadeTo(2000, 500).slideUp(500, function() {
                      $("#validationAlert").slideUp(1500);
                  });
                  return false;

              } else if (lastName == "") {
                  $("#lastName").focus();
                  alert("2");
                  $("#validationAlert").fadeTo(2000, 500).slideUp(500, function() {
                      $("#validationAlert").slideUp(1500);
                  });
                  return false;
              } else if (email == "") {
                  document.getElementById('email').focus();
                  $("#validationAlert").fadeTo(2000, 500).slideUp(500, function() {
                      $("#validationAlert").slideUp(1500);
                  });
                  return false;
              }
              return true;

          });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form method="POST" action="Login" onsubmit="return(showAlert());">
    <div class="form-group md-form">
        <!--<input type="email" class="form-control" id="email" value="" placeholder="Enter email address">-->
        <i class="fa fa-pencil prefix grey-text"></i>
        <input type="text" id="firstName" class="form-control">
        <label for="defaultForm-email">First Name</label>
    </div>

    <div class="form-group md-form">
        <!--<input type="email" class="form-control" id="email" value="" placeholder="Enter email address">-->
        <i class="fa fa-pencil prefix grey-text"></i>
        <input type="text" id="lastName" name="lastname" class="form-control">
        <label for="defaultForm-email">Last Name</label>
    </div>

    <div class="form-group md-form">
        <!--<input type="email" class="form-control" id="email" value="" placeholder="Enter email address">-->
        <i class="fa fa-envelope prefix grey-text"></i>
        <input type="email" id="email" name="email" class="form-control">
        <label for="defaultForm-email">Email Address</label>
    </div>

    <div class="text-center">
        <button type="reset" class="btn btn-amber btn-sm"><strong>Reset</strong></button>
        <!--<button type="submit" id="btn-sign-in" class="btn btn-green btn-sm"><strong>Sign in</strong></button>-->
        <button type="submit" id="btn-sign-up" class="btn btn-green btn-sm"><strong>Sign Up</strong></button>
    </div>
</form>
&#13;
&#13;
&#13;

谁能告诉我哪里出错了?

2 个答案:

答案 0 :(得分:1)

您的情况表明,如果您的名字,姓氏和电子邮件都是空的,则只会执行警报代码。

可能你需要以其他方式检查你的情况,

transformData()

答案 1 :(得分:0)

最后,我找到了自己的错。单击按钮后(而不是之前),应检查我输入/不输入的值。所以script应该是这样的。

    <script>
            $("#validationAlert").hide();
            $("#btn-sign-up").click(function showAlert() {
                var firstName = document.getElementById('firstName').value;
                var lastName = document.getElementById('lastName').value;
                var email = document.getElementById('email').value;
                var userName = document.getElementById('username').value;
                var password = document.getElementById('password').value;
                var repeatPassword = document.getElementById('repeatpassword').value;
                var securityQuestion = document.getElementById('securityQuestion').value;
                var answer = document.getElementById('answer').value;
                if (firstName == "") {
//                        alert("URL is mandatory!");
                    $("#firstName").focus();
                    alert(firstName);
                    $("#validationAlert").fadeTo(2000, 500).slideUp(500, function () {
                        $("#validationAlert").slideUp(1500);
                    });
                    return false;

                } else if (lastName == "") {
                    $("#lastName").focus();
                    alert("2");
                    $("#validationAlert").fadeTo(2000, 500).slideUp(500, function () {
                        $("#validationAlert").slideUp(1500);
                    });
                    return false;
                } else if (email == "") {
                    document.getElementById('email').focus();
                    $("#validationAlert").fadeTo(2000, 500).slideUp(500, function () {
                        $("#validationAlert").slideUp(1500);
                    });
                    return false;
                } else if (userName == "") {
                    document.getElementById('username').focus();
                    $("#validationAlert").fadeTo(2000, 500).slideUp(500, function () {
                        $("#validationAlert").slideUp(1500);
                    });
                    return false;
                } else if (password == "") {
                    document.getElementById('password').focus();
                    $("#validationAlert").fadeTo(2000, 500).slideUp(500, function () {
                        $("#validationAlert").slideUp(1500);
                    });
                    return false;
                } else if (repeatPassword == "") {
                    document.getElementById('repeatpassword').focus();
                    $("#validationAlert").fadeTo(2000, 500).slideUp(500, function () {
                        $("#validationAlert").slideUp(1500);
                    });
                    return false;
                } else if (securityQuestion == "-1") {
                    document.getElementById('securityQuestion').focus();
                    $("#validationAlert").fadeTo(2000, 500).slideUp(500, function () {
                        $("#validationAlert").slideUp(1500);
                    });
                    return false;
                } else if (answer == "") {
                    document.getElementById('answer').focus();
                    $("#validationAlert").fadeTo(2000, 500).slideUp(500, function () {
                        $("#validationAlert").slideUp(1500);
                    });
                    return false;
                }
                return true;


            });

   </script>