[英]How can I submit tr:inputText javascript when enter is pressed
[英]Javascript to trigger submit when enter is pressed
我有一个密码表单,我想按Enter键来触发“提交”按钮,但似乎无法正常工作。 我知道他们可以查看源代码来查看密码,这只是作为示例。
<!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,操作,提交类型,阻止默认设置和侦听器。 查看我的代码段。
祝你今天愉快!
<!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>
在您的表单中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.