2010-11-25 73 views
1

我這樣的代碼如何自動按 「Tab」 鍵,當用戶按下 「 - 」(破折號)鍵在JavaScript

 function Tab(e) 
     { 
      var input = e.keyCode ? e.keyCode : e.charCode; 
      if ((input>=48 && input<=57) || (input==45)) 
      { 
       if (input==45) 
       { 
       //what should i do here? so, 
       //if user press "-", its auto tab to next textfield, 
        return false 
       } 
       else 
       return true; 
      } 
      else 
      return false; 
     } 

這裏我的HTML

<input type="text" name="a[]" maxlength='3' onkeypress="return Tab(event)"> 
<input type="text" name="a[]" maxlength='3' onkeypress="return Tab(event)"> 
<input type="text" name="a[]" maxlength='3' onkeypress="return Tab(event)"> 

我一直在尋找谷歌。但其返回類似的文章,這不是我要找的。
我有很多相似的文本字段,所以不可能包含我使用數組名稱的下一個文本字段的名稱原因。

對不起,如果我的英語不好,但我希望你明白我想

+0

只是編輯,如果有錯誤的話 – 2010-11-25 14:00:59

回答

2

你可以關注下輸入這樣的同級什麼:

HTML:

<input type="text" name="a[]" maxlength='3' onkeypress="return Tab(event, this)"> 
<input type="text" name="a[]" maxlength='3' onkeypress="return Tab(event, this)"> 
<input type="text" name="a[]" maxlength='3' onkeypress="return Tab(event, this)"> 

JS:

function Tab(e, inp) 
    { 
     var input = e.keyCode ? e.keyCode : e.charCode; 
     if ((input>=48 && input<=57) || (input==45)) 
     { 
      if (input==45) 
      { 
       //focus the next input if there is one 
       while(inp.nextSibling) 
       { 
        var inp=inp.nextSibling; 
        if(inp.nodeType===1 && inp.tagName.toLowerCase()=="input") 
        { 
         inp.focus(); 
         break; 
        } 
       } 
       return false 
      } 
      else 
      return true; 
     } 
     else 
     return false; 
    } 
0

只需要10秒就可以詢問google。你不能模擬Tab鍵,但有不同的解決方法(也許你可以使用一個數組包含所有你的字段的ID,保存索引,按下破折號,在數組中重點索引+1)( +設置保存指數的onfocus每個文本字段,如果用戶通過點擊它還是真的壓片))

+0

當然你可以*模擬*選項卡,即使沒有tabindex。這並不完美,但請參閱[EmulateTab](https://github.com/joelpurra/emulatetab),以獲得將焦點移至下一個元素的一般方式 - 也[請嘗試簡單演示](http://joelpurra.github。 COM/emulatetab /示例/ demo.html)。 – 2012-02-17 11:59:35

0

我也曾有過類似的問題,在這裏我想按+上的數字鍵盤聚焦在現場要注意選項卡到下一個字段。現在我已經發布了一個我認爲會幫助你的圖書館。但確實需要

PlusAsTab:一個jQuery插件,將numpad plus鍵用作tab鍵等價物。

既然你想-(在正常的鍵,我猜),而不是,你可以設置的選項。找出您想要與jQuery event.which demo一起使用的密鑰。

JoelPurra.PlusAsTab.setOptions({ 
    // Use dash instead of plus 
    // Number 189 found through demo at 
    // https://api.jquery.com/event.which/ 
    key: 189 
}); 

// Matches all inputs with name "a[]" (needs some character escaping) 
$('input[name=a\\[\\]]').plusAsTab(); 

您可以try it out in the PlusAsTab demo,並檢查了enter-as-tab demo。如果您想更改爲-密鑰,您可以從您的JavaScript控制檯撥打JoelPurra.PlusAsTab.setOptions({key: 189});

相關問題