我编写了下面的代码来制作一个简单的表单,用于通过javascript验证表单输入。这里的用户名和密码都写在JS代码中,但即使在表单上提供正确的凭据,它仍会显示else循环的警报消息。 请帮帮忙?
var user = document.getElementById('username')
var pass = document.getElementById('password')
function user1() {
if (user == "admin" && pass == "root") {
window.open("javascript_trial.html")
alert('correct username')
} else {
alert('incorrect username or password')
}
}
<form>
<input type="text" name="username" Placeholder="enter username"><br>
<input type="password" name="password" Placeholder="enter password"><br>
<button onclick="user1()">Submit</button>
</form>
答案 0 :(得分:1)
这里有一些错误:
values
function user1() {
var user = document.getElementById('username').value
var pass = document.getElementById('password').value
if (user == "admin" && pass == "root") {
window.open("javascript_trial.html")
alert('correct username')
} else {
alert('incorrect username or password')
}
}
&#13;
<form>
<input type="text" name="username" id="username" Placeholder="enter username"><br>
<input type="password" name="password" id="password" Placeholder="enter password"><br>
<button onclick="user1()">Submit</button>
</form>
&#13;
另请注意,按钮的默认类型为submit
,它会在您点击后显示提醒时提交您的表单并重新加载页面,因此您可能希望将其更改为type="button"
防止这种情况。