使用Jquery在关键后关注下一个输入

注意:我的页面只有文本框。 没有其他的。 (没有别的=>没有其他输入类型)

$(":input[type='text']").keyup(function(event){ if(valid) { // take a focus to next input element, which is again a text type. } }); 

如何在按键后将焦点跳到下一个输入元素。

Sarfraz回答: –

  
// sarfraj -- here it crash Please check below for error.

从firebug问题是

  $(this).next("[type=\"text\"]")[0] is undefined [Break On This Error] $(this).next('[type="text"]')[0].focus(); 

更新:

以下是修改代码的方法:

 $(":input[type='text']").keyup(function(event){ if(valid) { if ($(this).next('[type="text"]').length > 0){ $(this).next('[type="text"]')[0].focus(); } else{ if ($(this).parent().next().find('[type="text"]').length > 0){ $(this).parent().next().find('[type="text"]')[0].focus(); } else { alert('no more text input found !'); } } } }); 

如何在按键后将焦点跳到下一个输入元素。

使用具有focusnext()

 $(this).next('[type="text"]')[0].focus(); 

所以这里是你的代码应该是这样的:

 $(":input[type='text']").keyup(function(event){ if(valid) { $(this).next('[type="text"]')[0].focus(); } }); 

html tabindex属性用于定义在单击选项卡时通过输入元素移动的顺序。 我会从设置这个开始。

然后设置选项卡索引(以展开您的示例):

  $(":input[type='text']").keyup(function(event){ if(valid) { var currentIndex = $(this).attr("tabindex"); var nextIndex = parseInt(currentIndex)+1; $("input[tabindex='"+nextIndex+"']").focus(); } }); 

基本上获取当前元素的tabindex ,添加1,并获取具有该选项卡索引的元素。