如何使用tab键跳转到某个输入?
目前,当用户在 qty
文本框中按 Tab 键时,它不会按 Tab 键切换到添加行图像。
我想添加一些 jQuery,以便当用户在 qty
文本框中按下 Tab 键时,它会切换到用户可以单击 Enter 的添加图像。
<td>
<input type="text" name="qty" id="qty" maxlength="3" class="txt" style="width: 80px;" />
</td>
<td>
<input type="image" src="/gfx/btn_addline.jpg" name="Add" value="Add" id="add"/>
</td>
Jquery影响数量:
$("#qty").keydown(function (event) {
//alert(event.keyCode);
if (event.keyCode == 13) {
$("#add").click();
return false;
}
});
$("#qty").keydown(function (event) {
// Allow only backspace and delete
if (event.keyCode == 46 || event.keyCode == 8) {
// let it happen, don't do anything
}
else {
// Ensure that it is a number and stop the keypress
if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
event.preventDefault();
}
}
});
Currently when the user presses tab in the qty
text box it does not tab to the add line image.
I want to add some jQuery in which would make it so that when the user presses the tab key within the qty
text box it tabs to the add image where the user can click enter.
<td>
<input type="text" name="qty" id="qty" maxlength="3" class="txt" style="width: 80px;" />
</td>
<td>
<input type="image" src="/gfx/btn_addline.jpg" name="Add" value="Add" id="add"/>
</td>
Jquery affecting qty:
$("#qty").keydown(function (event) {
//alert(event.keyCode);
if (event.keyCode == 13) {
$("#add").click();
return false;
}
});
$("#qty").keydown(function (event) {
// Allow only backspace and delete
if (event.keyCode == 46 || event.keyCode == 8) {
// let it happen, don't do anything
}
else {
// Ensure that it is a number and stop the keypress
if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
event.preventDefault();
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设我正确理解了您的问题,您应该只使用
tabindex
属性,它不需要任何 JavaScript:更新(参见评论)
如果您已经使用
tabindex
要控制页面上其他位置的 Tab 键顺序,只需在此处使用更高的索引即可。例如,如果索引 1-10 用于其他元素,请在此处使用 11 和 12:更新 2(请参阅更新的问题)
您正在阻止 Tab 键在
keydown< 中执行任何操作/code> 事件处理程序。更改为以下内容(添加另一个检查 Tab 键的条件):
假设您的两个输入在代码中按顺序排列并且它们之间没有输入,则无需
tabindex
即可工作。如果有,也使用tabindex
。作为旁注,您应该使用
event.which
而不是keyCode
,因为 jQuery 使用它来规范浏览器的不一致。Assuming I've understood your question correctly, you should just use the
tabindex
attribute, which doesn't require any JavaScript:Update (see comments)
If you already use
tabindex
to control tab order elsewhere on your page, just use higher indexes here. For example, if indexes 1-10 are used on other elements, use 11 and 12 here:Update 2 (see updated question)
You are preventing the tab key from doing anything in your
keydown
event handler. Change to the following (add another condition that checks for the tab key):This will work without the need for
tabindex
, assuming your two inputs are in order in your code and there are no inputs between them. If there are, usetabindex
as well.As a side note, you should use
event.which
instead ofkeyCode
, since jQuery uses it to normalise browser inconsistencies.我认为这可以解决问题
i think this will do the trick
您可以尝试将焦点和图像字段一起选择吗?
Can you try focus and select together for Image field.