好的,所以我不熟悉HTML中的整个Javascript。所以,我一直在研究一个简单的登录程序。由于某种原因,我的功能不起作用。每次我输入正确的用户名和密码时,它都会告诉我“您输入的用户名或密码不正确!”
<html>
<head>
<title>Login info stuff</title>
</head>
<body>
<script type="text/javascript">
var username = document.write("<input type='text' name='Username'>");
document.write("<br />");
var password = document.write("<input type='password' name='Password'>");
document.write("<br />");
function correct(){
if(username==="Username" & password==="Password")
{location="blank.HTML"}
else{alert("You entered your Username or Password incorrectly!")}
}
Submit = document.write("<input type='submit' value='Login' onClick='correct()'>");
</script>
</body>
</html>
答案 0 :(得分:1)
那是因为您比较的用户名和密码变量不是文本框中的值。这应该有效:
<html>
<head>
<title>Login info stuff</title>
</head>
<body>
<script type="text/javascript">
document.write("<input type='text' name='Username' id='username'>");
document.write("<br />");
document.write("<input type='password' name='Password' id='password'>");
document.write("<br />");
function correct(){
if(document.getElementById('username').value ==="Username" && document.getElementById('password').value === "Password")
{
location="blank.HTML";
}
else{
alert("You entered your Username or Password incorrectly!")
}
}
Submit = document.write("<input type='submit' value='Login' onClick='correct()'>");
</script>
</body>
</html>
我在这里做了什么:
id
document.getElementById
获取DOM元素,然后从value
属性中获取当前值答案 1 :(得分:0)
username
和password
变量存储document.write的返回值。他们没有检索到这些值。为此,您需要:
document.write("<input type=\"text\" name=\"username\" id=\"username\">");
var username = document.getElementById('username').value;
所以你的脚本应该是:
<script type="text/javascript">
document.write("<input type='text' name='Username' id='username'>");
document.write("<br />");
document.write("<input type='password' name='Password' id='password'>");
document.write("<br />");
function correct(){
if(document.getElementById('username').value==="Username" && document.getElementById('password').value==="Password")
{
location="blank.HTML"
} else {
alert("You entered your Username or Password incorrectly!")
}
}
Submit = document.write("<input type='submit' value='Login' onClick='correct()'>");
</script>
答案 2 :(得分:0)
AND在JavaScript中是&amp;&amp;
此外,您还需要将输入值保存为变量,如:
var user = document.getElementById('username').value;
var pwd = document.getElementById('password').value;
然后将这两个变量用于if语句
编辑:要使用getElementById()
,首先需要在document.write
行的相应输入中添加ID
答案 3 :(得分:0)
<html>
<head>
<title>Login info stuff</title>
</head>
<body>
<form id="myform" name="myform" action="blank.html" >
<input type="text" name="username" id="username" />
<input type="password" name="password" id="password" />
<input type="submit" name="submit" id="submit" value="Login" onclick="return correct();" />
</form>
<script type="text/javascript">
var username = document.getElementById("username"),
password = document.getElementById("password"),
myform = document.getElementById('myform');
function correct() {
if(username.value!=="username" || password.value!=="password") {
alert("You entered your Username or Password incorrectly!");
return false;
} else {
myform.submit();
}
}
</script>
</body>
</html>