Javascript 按下 enter 时触发提交
Javascript to trigger submit when enter is pressed
我有一个密码表单,我想要输入键来触发提交按钮,但我似乎无法让它工作。我知道他们可以查看源代码以查看密码这只是为了练习示例。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form>
<input id="user" type="text" name="username"
placeholder="username">
<input id="pass" type="password" id="pass" name="password">
<input id="subm"type="button" name="submit" value="submit"
onclick="checkPswd();">
</form>
</body>
<script type="text/javascript">
function checkPswd(){
var confirmpassword = "admin";
var username = document.getElementById('user').value;
var password = document.getElementById("pass").value;
if(password == confirmpassword){
window.location.href = "redu.html";
}else{
alert('try again');
}
};
</script>
</html>
只需将 input // submit
替换为 button
并使用 onSubmit
将函数移动到 form
元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form>
<input id="user" type="text" name="username"
placeholder="username" onSubmit="checkPswd()">
<input id="pass" type="password" id="pass" name="password">
<button type="submit">submit</button>
</form>
</body>
<script type="text/javascript">
function checkPswd(){
var confirmpassword = "admin";
var username = document.getElementById('user').value;
var password = document.getElementById("pass").value;
if(password == confirmpassword){
window.location.href = "redu.html";
}else{
alert('try again');
}
};
</script>
</html>
你需要添加id、action、submit type、prevent default和listener。查看我的代码片段..
祝你有愉快的一天!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!-- add id and action -->
<form id="example" action="#">
<input id="user" type="text" name="username" placeholder="username">
<input id="pass" type="password" id="pass" name="password">
<!-- change type to submit -->
<input id="subm" type="submit" name="submit" value="submit">
</form>
</body>
<script type="text/javascript">
//add listener
document.getElementById("example").addEventListener("submit", checkPswd, true);
function checkPswd(){
var confirmpassword = "admin";
var username = document.getElementById('user').value;
var password = document.getElementById("pass").value;
if(password == confirmpassword){
window.location.href = "redu.html";
}else{
alert('try again');
}
//add prevent default
event.preventDefault();
};
</script>
</html>
随便放一个
<button type="submit">Submit</button>
在您的表单中。
我有一个密码表单,我想要输入键来触发提交按钮,但我似乎无法让它工作。我知道他们可以查看源代码以查看密码这只是为了练习示例。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form>
<input id="user" type="text" name="username"
placeholder="username">
<input id="pass" type="password" id="pass" name="password">
<input id="subm"type="button" name="submit" value="submit"
onclick="checkPswd();">
</form>
</body>
<script type="text/javascript">
function checkPswd(){
var confirmpassword = "admin";
var username = document.getElementById('user').value;
var password = document.getElementById("pass").value;
if(password == confirmpassword){
window.location.href = "redu.html";
}else{
alert('try again');
}
};
</script>
</html>
只需将 input // submit
替换为 button
并使用 onSubmit
form
元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form>
<input id="user" type="text" name="username"
placeholder="username" onSubmit="checkPswd()">
<input id="pass" type="password" id="pass" name="password">
<button type="submit">submit</button>
</form>
</body>
<script type="text/javascript">
function checkPswd(){
var confirmpassword = "admin";
var username = document.getElementById('user').value;
var password = document.getElementById("pass").value;
if(password == confirmpassword){
window.location.href = "redu.html";
}else{
alert('try again');
}
};
</script>
</html>
你需要添加id、action、submit type、prevent default和listener。查看我的代码片段..
祝你有愉快的一天!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!-- add id and action -->
<form id="example" action="#">
<input id="user" type="text" name="username" placeholder="username">
<input id="pass" type="password" id="pass" name="password">
<!-- change type to submit -->
<input id="subm" type="submit" name="submit" value="submit">
</form>
</body>
<script type="text/javascript">
//add listener
document.getElementById("example").addEventListener("submit", checkPswd, true);
function checkPswd(){
var confirmpassword = "admin";
var username = document.getElementById('user').value;
var password = document.getElementById("pass").value;
if(password == confirmpassword){
window.location.href = "redu.html";
}else{
alert('try again');
}
//add prevent default
event.preventDefault();
};
</script>
</html>
随便放一个
<button type="submit">Submit</button>
在您的表单中。