使用jQuery更改文本字段焦点顺序

时间:2010-12-23 21:36:02

标签: jquery html textbox focus textfield

我想知道,如果我显示了文本字段,我可以使用javascript(最好是jQuery)改变焦点的顺序。

请参阅此示例here

我的意思是,当你围绕它们时,而不是“一,二,三,四,五”强迫它们去:“一,二,四,三,五”。

提前致谢!!

编辑:感谢所有人,我不知道只有HTML才有可能。当然,如果是这种情况,我会坚持下去。

BTW感谢您的所有答案! (我会提供完整的)但会接受第一个。

5 个答案:

答案 0 :(得分:3)

在元素上设置tabindex属性。

jsfiddle

答案 1 :(得分:3)

您可以使用tabIndex属性根据需要使用Tab键顺序: e.g:

<input type="text" tabIndex=1 value="one"/><br />
<input type="text" tabIndex=2 value="two"/>
<input type="text" tabIndex=4 value="three"/><br />
<input type="text" tabIndex=3 value="four"/>
<input type="text" tabIndex=5 value="five"/>

但是如果你想在客户端动态更改标签,你可以使用jQuery .attr函数。

$(<YOUR ELEMENT SELECTOR>).attr("tabIndex", "YOUR_VALUE");

答案 2 :(得分:3)

不需要jQuery!查看tabIndex

答案 3 :(得分:1)

只需设置tabindex属性,如下所示:

<input type="text" value="one" tabindex="1" /><br />
<input type="text" value="two" tabindex="2"/>
<input type="text" value="three" tabindex="4" /><br />
<input type="text" value="four" tabindex="3" />
<input type="text" value="five" tabindex="5" />

...不需要JavaScript。 You can test it here

答案 4 :(得分:1)

您可以使用jQuery tabindex设置 attr method 属性:

在您的情况下,我们需要使用eq selector,因为您的元素没有ID。

$("input:eq(0)").attr("tabindex","1");
$("input:eq(1)").attr("tabindex","2");
$("input:eq(2)").attr("tabindex","4");
$("input:eq(3)").attr("tabindex","3");
$("input:eq(4)").attr("tabindex","5");

Try it here!