【发布时间】:2015-09-14 13:12:55
【问题描述】:
我想要做的是,当我按下回车键时,焦点(光标)移动到下一个输入字段,我得到了。在下面的代码中,当按下 Enter 时,我将表单的提交设置为 false,这样表单就不会在输入时提交。但结果是当我点击提交按钮时表单甚至没有提交。 我所需要的只是输入我的光标移动到下一个输入字段,然后单击按钮提交表单。我希望这对你有意义..
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"> </script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script>
$(document).ready(function() {
$('.username').keypress(function (e) {
if (e.which === 13) {
var index = $('.username').index(this) + 1;
$('.username').eq(index).focus();
}
});
});
</script>
</head>
<body>
<div class="test">
<form method="POST" id="form">
<input type="text" class="username" onkeypress="sheikh(event)">
<input type="text" class="username">
<input type="text" class="username">
<input type="text" class="username">
<input type="text" class="username">
<input type="text" class="username">
<input type="submit" value="Submit" class="username">
</form>
</div>
<script>
function sheikh(e){
var key;
if(e.which == 13){
document.getElementById("form").onsubmit=function (){
return false;
}
}
else{
document.getElementById("form").onsubmit=function (){
return true;
}
}
}
</script>
</body>
</html>
【问题讨论】:
-
你试过
if (e.which === 13) { e.preventDefault() ........?
标签: javascript jquery html forms