我一直在尝试使用文本框和按钮创建登录系统。您应该在文本框中输入密码,然后按下按钮,但是当我这样做时,没有任何反应。到目前为止,这是我的代码:
<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>
答案 0 :(得分: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>
答案 1 :(得分: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
答案 2 :(得分: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);
on jsfiddle