2012-05-09 66 views
3

通常,當用戶訪問網頁並按下鍵盤上的TAB按鈕時,選擇將從頁面開始時的一個元素移動到另一個元素。僅在按下Tab按鈕時在兩個文本區之間切換

我正在尋找一種解決方案,通過按下鍵盤上的Tab鍵在初始焦點上加載網頁時的第一個文本區域之間切換兩個特定的文本區域?對於此TAB鍵按下事件,頁面上的所有其他元素都必須被忽略。

我該如何實現這一目標?

謝謝你的幫助!

=更新=

我設法使其在Firefox 12.0下工作。 IE和Chrome無法正常工作。 Asuming文本區域ID被#ICCID和#MSISDN,Jquery的看起來像這樣:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(function() { 
      $(document).ready(function() { 
      $("#ICCID").focus(); 
      }); 

      var $inp = $('.cls'); 

      $inp.bind('keydown', function(e) { 
       var key = e.which; 
       if (key == 9) { 

        e.preventDefault(); 
        var nxtIdx = $inp.index(this) + 1;     
        $(".cls:eq(" + nxtIdx + ")").focus(); 

       //Simulate Enter after TAB 
       var textInput = $("#MSISDN").val(); 
       var lines = textInput .split(/\r|\r\n|\n/); 
       if (lines > 1) { 

        $("#MSISDN").on("keypress", function(e) { 
        if (e.keyCode == 9) { 
        var input = $(this); 
        var inputVal = input.val(); 
        setTimeout(function() { 
        input.val(inputVal.substring(0,inputVal.length) + "\n"); 
          }, 1); 
         } 
        }); 
       } 



       } 
       if (key == 9) { 

        e.preventDefault(); 
        var nxtIdx = $inp.index(this) - 1; 
        $(".cls:eq(" + nxtIdx + ")").focus(); 

       //Simulate Enter after TAB 
       $("#ICCID").on("keypress", function(e) { 
       if (e.keyCode == 9) { 
       var input = $(this); 
       var inputVal = input.val(); 
       setTimeout(function() { 
       input.val(inputVal.substring(0,inputVal.length) + "\n"); 
         }, 1); 
        } 
       }); 


       } 
      }); 
     }); 
</script> 
+4

不要!請!有些人需要能夠使用標籤將其他元素集中在頁面中!不是每個人都可以使用鼠標。 – Quentin

+0

@Quentin。或者想要你的鼠標(就像我......) – gdoron

+0

只是爲textarea定義了兩個連續的tabindex屬性,但是避免了爲每個其他元素禁用焦點 – fcalderan

回答

2

好,我已經找到了解決方案,爲我的任務!它還包括在TAB鍵事件之後模擬ENTER鍵,因此用戶不需要按ENTER鍵即可進入新行。與IE9,FF12,鉻18.0.x

這裏測試,它是:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 

<!-- Switching between ICCIDs and MSISDNs textareas + simulating ENTER key pressing after the TAB key event - START --> 
    <script type="text/javascript"> 
      $(function() { 
       $(document).ready(function() { 
       $("#ICCID").focus(); 
       }); 

       var $inp = $('.cls'); 

       $inp.bind('keydown', function(e) { 
        var key = e.which; 
        if (key == 9) { 

         e.preventDefault(); 
         var nxtIdx = $inp.index(this) + 1;     
         $(".cls:eq(" + nxtIdx + ")").focus(); 

        //Simulate Enter after TAB 
        var textInput = $("#MSISDN").val(); 
        var lines = textInput .split(/\r|\r\n|\n/); 
        if (lines > 1) { 

         $("#MSISDN").on("keyup", function(e) { 
         if (e.keyCode == 9 || e.which == 9) { 
         var input = $(this); 
         var inputVal = input.val(); 
         setTimeout(function() { 
         input.val(inputVal.substring(0,inputVal.length) + "\r\n"); 
           }, 1); 
          } 
         }); 
        } 



        } 
        if (key == 9) { 

         e.preventDefault(); 
         var nxtIdx = $inp.index(this) - 1; 
         $(".cls:eq(" + nxtIdx + ")").focus(); 

        //Simulate Enter after TAB 
        $("#ICCID").on("keyup", function(e) { 
        if (e.keyCode == 9 || e.which == 9) { 
        var input = $(this); 
        var inputVal = input.val(); 
        setTimeout(function() { 
        input.val(inputVal.substring(0,inputVal.length) + "\r\n"); 
          }, 1); 
         } 
        }); 


        } 
       }); 
      }); 
    </script> 
    <!-- Switching between ICCIDs and MSISDNs textareas + simulating ENTER key pressing after the TAB key event - END --> 
2

捕捉使用jQuery該keydown動作,確定哪些文本區域具有焦點,然後使用焦點()方法來設置焦點到另一個textarea。

假設你的textareas有id =「textarea1」和id =「textarea2」。首先,你可以通過做焦點設置爲第一textarea的頁面加載時:$('#textarea1').focus();

$("body").keypress(function(e) { 
    var code = (e.keyCode ? e.keyCode : e.which); 
    switch(code) 
    { 
     case 9: 
      if($("#textarea1").focus()){ 
       //First one has focus, change to second one 
       $("#textarea2").focus(); 
      } 
      else if($("#textarea2").focus()) { 
       //Second one has focus, change to first one 
       $("#textarea1").focus(); 
      } 

    } 
}); 
+0

我明白它應該如何工作,但我是jQuery的新手。有人可以生成代碼嗎? –

+0

我添加了一些代碼,應該做你想要的。你是否熟悉像'$('#myTextArea1')'這樣的選擇器? –

+0

我確實收到了一條提示,說'按下了Tab鍵!'。這次真是萬分感謝。現在你能幫我用兩個textarea字段之間的代碼交換嗎? 第一個字段名稱=「ICCID」,另一個名稱=「MSISDN」。如果需要,我可以引入ID。感謝名單! –

0

使用在頁面加載的javascript:

document.getElementById("textarea1").focus(); 
document.getElementById('textarea1').tabIndex="1"; 
document.getElementById('textarea2').tabIndex="2"; 
+0

此解決方案部分工作。它不做主要的事情,TAB按鈕對其他元素仍然有效。 –

+0

您可以獲取keydown事件並綁定選項卡鍵。 – Adam

1

這個怎麼樣....林在工作中我無聊想..

http://jsbin.com/uqalej/3/

HTML:

<input/> 
<textarea id="t1"></textarea> 
<textarea id="t2"></textarea> 
<input/> 

<button onClick='window.toggleBetween=true;'>Init</button> 
<button onClick='window.toggleBetween=false;'>Destroy</button> 

JS:

var d = document, 
    t1 = d.getElementById("t1"), 
    t2 = d.getElementById("t2"), 

    nodeType, nodeTypes = [], 
    i, iLen, 
    y, yLen; 

nodeTypes.push(d.getElementsByTagName("textarea")); 
nodeTypes.push(d.getElementsByTagName("input")); 
nodeTypes.push(d.getElementsByTagName("select")); 

i = 0; 
iLen = nodeTypes.length; 
for (; i < iLen; i++) { 
    nodeType = nodeTypes[i]; 
    y = 0; 
    yLen = nodeType.length; 
    for (; y < yLen; y++) { 
    if (nodeType[y] != t1 && nodeType[y] != t2) { 
     nodeType[y].onfocus = function() { 
     if (window.toggleBetween) 
      t1.focus(); 
     }; 
    } 
    } 
} 
+0

適用於您的示例,但不適用於我的頁面。看起來像是因爲我在那裏得到了一些表單字段驗證。 –

+0

這裏是我使用的表單驗證的代碼:。 <腳本類型= 「文本/ JavaScript的」> 功能validateForm() { 變種X = document.forms [ 「SimForm」] [ 「用戶ID」]值; var x = document.forms [「SimForm」] [「UserName」] .value; var x = document.forms [「SimForm」] [「SimCredit」] .value; var x = document.forms [「SimForm」] [「SimNetwork」] .value; var x = document.forms [「SimForm」] [「SimStatus」] .value; var x = document.forms [「SimForm」] [「SIMLOCATION」] .value; if(x == null || x ==「」|| x ==「NotSelected」) { alert(「Please note,all fields are required!」); 返回false; } } –

+0

你必須設置'window.toogleBetween = true'。但這只是一個實驗性的代碼。不建議使用它.. – andlrc

相關問題