HTML+JS 阻止表单的(按下回车键)自动提交
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<title>Document</title>
</head>
<body>
<form onsubmit="return false">
<input type="text" id="usname" value="sd" >
</form>
<script>
var form=document.getElementsByTagName("form") ;
form.addEventListener("submit",function(e){
e.preventDefault();//阻止元素的默认行为
});
$("#usname").keydown(function(event){
if(event.keyCode==13){
console.log('你按下了Enter 并再文本框框里输入了'+this.value);
}
});
</script>
</body>
</html>
页:
[1]