如何使用jQuery自动选项到下一个字段? [英] How can I automatically tab to the next field using jQuery?
问题描述
在jQuery中,如何触发用户标签到下一个输入字段的行为?
In jQuery, how can I trigger the behavior of a user tabbing to the next input field?
我试过这个:
var e = jQuery.Event("keydown");
e.which = 9; // # Key code for the Tab key
$("input").trigger(e);
但是触发事件不会将光标移动到下一个字段。
But triggering the event doesn't move the cursor to the next field.
我想我可以使用 focus()
手动移动光标,但决定下一个应该是哪个字段是浏览器已经知道该怎么做了,所以触发标签似乎更清晰。
I suppose I could move the cursor manually using focus()
, but deciding which field should be next is something the browser already knows how to do, so it seems much cleaner to just trigger a tab.
任何想法?
推荐答案
查看这个问题。例如,如果您想在输入一定数量的字符时将焦点移动到下一个字段,则可以在 keyup
事件中使用该代码,并检查输入的数字字符。
See the accepted answer to this question. If for example you want to move focus to the next field when a certain number of characters have been entered, you could use that code in the keyup
event, and check the entered number of characters.
该答案中的代码通过获取表单中的输入集,查找所选输入并将1添加到所选输入的索引来工作。然后在具有该索引的元素上触发焦点
事件。
The code in that answer works by getting the set of inputs in the form, finding the selected input and adding 1 to the index of the selected input, and then triggering the focus
event on the element with that index.
这篇关于如何使用jQuery自动选项到下一个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!