-1

我一直在尝试使用文本框和按钮制作登录系统。您应该在文本框中输入密码,然后按下按钮,但是当我这样做时没有任何反应。到目前为止,这是我的代码:

<html>
<body>
<script type="text/javascript">
function EnterPwerd() {
var pass= input = document.getElementById("text")
if ( (pass=='bacon') ) {
document.write("BACON is so good.")
}
else {
document.write("You put the wrong password you taco biscuit!")
}
</script>
<div id="text"><input type="text" value="" size="10" maxlength="15"></civ>
<input type="button" value="Enter" onClick="EnterPwerd()">
</body>
</html>
4

3 回答 3

0
<div id="text">
    <input id="password" type="password" value="" size="10" maxlength="15">
</div>
<input id="button" type="button" value="Enter">

var passbox = document.getElementById("password");

function enterPwerd() {
    var pass = passbox.value;

    if (pass === 'bacon') {
        alert("BACON is so good.")
    } else {
        alert("You put the wrong password you taco biscuit!")
    }
}

document.getElementById("button").addEventListener("click", enterPwerd, false);

jsfiddle上

于 2013-04-20T19:47:43.913 回答
0

如果要获取输入元素值,则需要获取输入元素 id。尝试这个:

<html>
<head>
    <script type="text/javascript">
        function EnterPwerd() {
            var pass = document.getElementById("text").value;
            if ( (pass=='bacon') ) {
                document.write("BACON is so good.");
            }
            else {
                document.write("You put the wrong password you taco biscuit!");
            }
        }
    </script>
</head>
<body>
    <input type="text" value="" size="10" maxlength="15"  id="text">
    <input type="button" value="Enter" onClick="EnterPwerd()">
</body>
</html>
于 2013-04-20T19:42:29.433 回答
0
your closing div is a typo (</civ>)
You can better do as
Add an id of textbox
<div id="text"><input type="text" value="" size="10" maxlength="15" id= "pwd"></div>

hold password as
var pass= document.getElementById("pwd")

also for password use type="password" to hide letters
于 2013-04-20T19:44:47.917 回答