没有点击功能

时间:2019-03-01 07:01:15

标签: javascript html

if语句中的消息不起作用,我也不知道为什么。似乎当单击“提交”按钮时,正文代码也不会通过javascript代码。有人可以解释一下吗?

    <script>
        function checkSignup() {
            var name = document.getElementById("username").value;
            var p1 =  document.getElementById("password1").value;
            var p2 =  document.getElementById("password2").value;
            var namelength = name.length;
            var pass1 = toString(p1);
            var pass2 = toString(p2);
            var passlength = pass1.length;

            if (namelength < 8 || namelength > 15) {
                document.getElementById("error1").innerHTML = "8 to 15 characters only";
            }
            if (passlength < 8){
                document.getElementById("error2").innerHTML = "minimum of 8 characters";
            }
            if (pass1 != pass2){
                document.getElementById("error3").innerHTML = "should be the same with password";
            }
            if (namelengh >= 8 && namelength <= 15 && p1length >= 8 && p1 == p2)
                document.getElementbyId("error1").innerHTML = "";
                document.getElementbyId("error2").innerHTML = "";
                document.getElementbyId("error3").innerHTML = "";
                var confirm = window.confirm("Are you sure you want to submit?");
                if (confirm == 1){
                    alert("You are now sign-up!");
                }
        }
    </script>
</head>
<body>
    <form method = "post" onsubmit = "return false">
        <fieldset>
        <legend>New Account Screen</legend>
            Username: <input type = "text" autofocus required id = "username"><p id = "error1"></p>
            <br>
            Password:<input type = "password" id = "password1" required><p id = "error2"></p>
            <br>
            Re-enter <br>
            Password: <input type = "password" id = "password2" required><p id = "error3"></p>
            <div id = "div1">
            <input type = "submit" value = "Submit" onclick = "checkSignup()">
            <input type = "reset" value = "Reset">
            </div>
        </fieldset>
    </form>
</body>

4 个答案:

答案 0 :(得分:0)

namelengh未定义。

namelengh更改为namelength

if (namelengh >= 8 && namelength <= 15 && p1length >= 8 && p1 == p2)

答案 1 :(得分:0)

像这样的小错误 document.getElementById b应该是大写且不能小。 namelength的if条件之一有错字

var pass1='',pass2='',passlength=0,namelength=0;
        function checkSignup() {
            var name = document.getElementById("username").value;
            var p1 =  document.getElementById("password1").value;
            var p2 =  document.getElementById("password2").value;
             namelength = name.length;
             pass1 = toString(p1);
             pass2 = toString(p2);
             passlength = pass1.length;

            if (namelength < 8 || namelength > 15) {
                document.getElementById("error1").innerHTML = "8 to 15 characters only";
            }
            if (passlength < 8){
                document.getElementById("error2").innerHTML = "minimum of 8 characters";
            }
            if (pass1 != pass2){
                document.getElementById("error3").innerHTML = "should be the same with password";
            }
            if (namelength >= 8 && namelength <= 15 && p1length >= 8 && p1 == p2)
                document.getElementById("error1").innerHTML = "";
                document.getElementById("error2").innerHTML = "";
                document.getElementById("error3").innerHTML = "";
                var confirm = window.confirm("Are you sure you want to submit?");
                if (confirm == 1){
                    alert("You are now sign-up!");
                }
        }
<body>
    <form method = "post" onsubmit = "return false">
        <fieldset>
        <legend>New Account Screen</legend>
            Username: <input type = "text" autofocus required id = "username"><p id = "error1"></p>
            <br>
            Password:<input type = "password" id = "password1" required><p id = "error2"></p>
            <br>
            Re-enter <br>
            Password: <input type = "password" id = "password2" required><p id = "error3"></p>
            <div id = "div1">
            <input type = "submit" value = "Submit" onclick = "checkSignup()">
            <input type = "reset" value = "Reset">
            </div>
        </fieldset>
    </form>
</body>

答案 2 :(得分:0)

几乎没有打字错误,这是功能齐全的代码。

 function checkSignup() {
        var name = document.getElementById("username").value;
        var p1 = document.getElementById("password1").value;
        var p2 = document.getElementById("password2").value;
        var namelength = name.length;
        var pass1 = toString(p1);
        var pass2 = toString(p2);
        var passlength = pass1.length;

        if (namelength < 8 || namelength > 15) {
            document.getElementById("error1").innerHTML = "8 to 15 characters only";
        }
        if (passlength < 8) {
            document.getElementById("error2").innerHTML = "minimum of 8 characters";
        }
        if (pass1 != pass2) {
            document.getElementById("error3").innerHTML = "should be the same with password";
        }
        if (namelength >= 8 && namelength <= 15 && p1.length >= 8 && p1 == p2)
        document.getElementById("error1").innerHTML = "";
        document.getElementById("error2").innerHTML = "";
        document.getElementById("error3").innerHTML = "";
        var confirm = window.confirm("Are you sure you want to submit?");
        if (confirm == 1) {
            alert("You are now sign-up!");
        }
    }
<body>
    <form method="post" onsubmit="return false">
        <fieldset>
            <legend>New Account Screen</legend>
            Username: <input type="text" autofocus required id="username">
            <p id="error1"></p>
            <br>
            Password:<input type="password" id="password1" required>
            <p id="error2"></p>
            <br>
            Re-enter <br>
            Password: <input type="password" id="password2" required>
            <p id="error3"></p>
            <div id="div1">
                <input type="submit" value="Submit" onclick="checkSignup()">
                <input type="reset" value="Reset">
            </div>
        </fieldset>
    </form>
</body>

答案 3 :(得分:-1)

变量名称 namelength 在下面的if语句中错误

if (namelengh >= 8 && namelength <= 15 && p1length >= 8 && p1 == p2)

将其更改为namelength并尝试。