【发布时间】:2019-01-30 18:14:43
【问题描述】:
我首先要说我对 javascript、jquery 和 php 还很陌生。虽然我比其他 2 更熟悉 PHP,但我仍然只是一个初学者。 所以我正在尝试制作这个 HTML 表单,以便人们可以提交简短的评论。
我的目标是使用它的人可以使用 enter 在输入框之间切换,但只能在具有相同类的特定输入框之间切换。否则,可以按回车键提交表单。 (不会出现提交按钮)。
这是我的 HTML(只是一个试用,还不是真的)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Help </title>
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$("input").bind("keydown", function(event) {
if (event.which === 13) {
alert("asd");
event.stopPropagation();
event.preventDefault();
$(':input:eq(' + ($(':input').index(this) + 1) +')').focus();
}
});
</script>
</head>
<body>
<h3> welcome to the help page </h3>
<form action= "Helppage.php" method="post" ">
<p><b> name: </b></br>
<input class= "code" type="text" name="name" size= "20" ></p>
<p><b> location: </b> </br>
<input class= "code" type= "text" name="location" size= "20" > </p>
<p><b> message:</b></br>
<input class= "code" type= "text" name="message"size= "20" ></p>
<p><b> remark:</b></br>
<input type= "text" name="remark"size= "20" ></p>
<p><b> department:</b></br>
<input type= "text" name="department"size= "20" ></p>
</form>
<h3> thanks for participating!!</h3>
</body>
</html>
我已经尝试找到解决方案。 喜欢这个:How to use enter key to focus into next input 还有这个:Activating next input field in form on enter
但由于某种原因,我似乎无法让它们中的任何一个工作......
提前致谢!
【问题讨论】:
-
您已通过有效答案链接到类似问题,但没有告诉我们您的实施中为什么/什么不起作用。请展示您的实现,否则我们将无能为力!
-
使用第一个链接中的代码,但使用
$("input")而不是"$("input,select")并在您使用它时将您的HTML 更改为小写。 -
所以我添加了在我给出的示例之一中使用的代码。但我似乎无法弄清楚如何让它通过具有相同类别的盒子。它只是....提交表单:s.
-
您的
<form>中有一个错字,一个双引号"太多了。只是说! -
您尝试在加载 dom 之前 绑定事件监听器。这就是 jQuerys document ready 的用途!
标签: javascript php jquery html