2

Note: My page has just textboxes. Nothing else. (nothing else => no other input types)

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

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

  });

How can i jump the focus to next input element after key up.

After Sarfraz Answer:-

 <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>

From firebug issue is

  $(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()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プロパティは、タブを押した場合に入力要素を移動する順序を定義するために使用されます。これを設定することから始めます。

したがって、タブインデックスを設定します(例を拡張するため)。

 $(":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 に答える