//当回车键提交时聚焦到当时触发onkeypress事件的元素对象年龄输入框
<script type="text/javascript">
$(function(){
$("input").keypress(function(e){
var keyCode=e.keyCode?e.keyCode:e.which?e.which:e.charCode;
if(keyCode==13){
for(var i=0;i<this.form.elements.length;i++){
if (this==this.form.elements[i]) break;
}
i=(i+1)%this.form.elements.length;
this.form.elements[i].focus(); //聚焦到提交按钮上
return false;
}else{
return true;
}
}
)
});
</script>
</head>
<body>
<form action="form.php" method="post">
Name:<input type="text" name="name"/>
Age:<input type="text" name="age"/>
<input type="submit" value="提交"/>
</form>
</body>
时间: 2024-10-14 10:26:21