2

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

 $(":input[type='text']").keyup(function(event){

   if(valid)
     {
         // take a focus to next input element, which is again a text type.
     }

  });

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

在 Sarfraz 之后回答:-

 <div>
   <input type="text" maxlength="1" size="1" >
   <input type="text" maxlength="1" size="1" > 
   <input type="text" maxlength="1" size="1" > // sarfraj -- here it crash   Please check below for error.
 </div>


 <div>
   <input type="text" maxlength="1" size="1" >
   <input type="text" maxlength="1" size="1" >
   <input type="text" maxlength="1" size="1" >
 </div>

从萤火虫问题是

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

2 回答 2

5

更新:

以下是您应该如何修改代码:

$(":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 !');
         }
      }

   }
});

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

使用next()with focus

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

因此,您的代码应如下所示:

$(":input[type='text']").keyup(function(event){
   if(valid) {
      $(this).next('[type="text"]')[0].focus();
   }
});
于 2011-08-27T21:10:59.037 回答
3

htmltabindex属性用于定义按 Tab 键时在输入元素中移动的顺序。我会从设置这个开始。

因此,然后设置选项卡索引(以扩展您的示例):

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

基本上得到tabindex当前元素的,加1,得到那个标签索引的元素。

于 2011-08-27T21:13:43.433 回答