【问题标题】:Javascript to trigger submit when enter is pressed按下输入时触发提交的Javascript
【发布时间】:2018-12-07 00:20:10
【问题描述】:
我有一个密码表单,我希望输入键触发提交按钮,但我似乎无法让它工作。我知道他们可以查看源代码以查看密码,这只是为了练习示例。
<!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>
【问题讨论】:
-
我已删除您的java 问题标签。请理解 Java 和 JavaScript 是两种完全不同的语言,问题标签和问题标题是您问题中最重要的部分,如果其中任何一个被关闭,您将无法让正确的专家来查看您的问题。
-
标签:
javascript
html
css
enter
【解决方案1】:
你需要添加 id、action、submit type、prevent default 和 listener。看看我的 sn-p..
祝你有美好的一天!
<!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>
【解决方案2】:
只需将您的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>
【解决方案3】:
放一个
<button type="submit">Submit</button>
在您的表单中。