2016-09-27 25 views
0

我有一個webform,並希望用戶能夠在按下向下箭頭(行爲與tab鍵相同)時轉到下一個tabindex字段。當按下回車鍵時,下列代碼有效,但如果將鍵碼更改爲40,則此代碼對於下箭頭鍵不起作用。在向下箭頭鍵上選項卡到下一個tabindex字段

任何幫助非常感謝。

<div> 
    <input name="line[]" class="" type="text" tabindex="1"/> 
</div> 
<div> 
    <input name="line[]" class="" type="text" tabindex="2"/> 
</div> 
<div> 
    <input name="line[]" class="" type="text" tabindex="3"/> 
</div> 

//tab to next tabindex on enter key 
<script> 
    var id; 
    $(document).ready(function(eOuter) { 
     $('input').bind('keypress', function(eInner) { 
      if (eInner.keyCode == 13){ //enter key 
       var tabindex = $(this).attr('tabindex'); 
       tabindex++; //increment tabindex 
       $('[tabindex=' + tabindex + ']').focus(); 

       $('#Msg').text($(this).attr('id') + " tabindex: " + tabindex + " next element: " + $('*').attr('tabindex').id); 
      } 
     }); 
    }); 
</script> 

回答

1

鍵不上keypress始終如一事件觸發的箭頭,你想用​​,而不是

$('input').on('keydown', function(eInner) { 

    if (eInner.which === 40) { //arrow down 

FIDDLE

您的消息有一定的問題,以及,元素穿上」沒有ID,jQuery的attr返回一個沒有id屬性的原語等。

+0

謝謝作品和我學到了一些東西。編號將被添加。 – user3058870

相關問題