HTML代码
<!DOCTYPE html><html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div>
用户名 : <input type="text" name="username" id="username_input"></br>
密码 : <input type="password" name="passwd" id="passwd_input"></br>
<input type="button" value="登录" onclick="login()">
</div>
</body>
</html>
Js代码
<script type="text/javascript">var accountAll = [
{'username' : 'admin','passwd' : 'admin123'},
{'username' : 'jack','passwd' : 'jack'}
] // 存储账户json数据的数组
function login(){ //登陆判断
var username = document.getElementById('username_input').value // 获取username输入框内容
var passwd = document.getElementById('passwd_input').value // 获取passwd输入框内容
var account = accountAll.filter(function(e){return e.username == username})[0] // 筛选账号返回数组,不存在则返回空数组
if(!account){
console.log('账户不存在')
} else {
if(account.username == username && account.passwd == passwd){
console.log('登陆成功')
} else {
console.log('登陆失败')
}
}
}
</script>
function login(){if (document.login.usename.value== "logon" && document.login.password.value =="logon") {
location.href="main.html"
}else{
return false
}
}
function login(){
var username=document.getElementById("userName")
var password=document.getElementById("password")
if(username.value=="admin"&&password.value="123456")
{
window.location.href="success.html"
}
}
为登录按钮注册onclick=“login()”