简体   繁体   English

向下箭头键上的 Tab 到下一个 tabindex 字段

[英]Tab to next tabindex field on down arrow key

I have a webform and want the users to be able to go to the next tabindex field when the down arrow is pressed (behave like the tab key).我有一个网络表单,并希望用户能够在按下向下箭头时转到下一个 tabindex 字段(表现得像 tab 键)。 The following code works when enter key is pressed but if I change the keycode to 40 this code will not work for the down arrow key.以下代码在按下 Enter 键时有效,但如果我将键码更改为 40,此代码将不适用于向下箭头键。

Any help greatly appreciated.非常感谢任何帮助。

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

The arrow keys don't fire consistently on the keypress event, you want to use keydown instead箭头键不会在keypress事件上持续触发,您想改用keydown

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

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

FIDDLE小提琴

Your message has some issues as well, the elements don't have ID's, jQuery's attr returns a primitive that has no id property etc.您的消息也有一些问题,元素没有 ID,jQuery 的attr返回一个没有id属性的原语等。

100% working for up arrow and down arrow 100%up arrowdown arrow

$(document).ready(function(eOuter) {
     $('input').on('keydown', function(eInner) {
         var keyValue = eInner.which; //enter key
         if (keyValue == 40 || keyValue == 38){ 
             var tabindex = $(this).attr('tabindex');
             if(keyValue == 40){ //down arrow 40
                 tabindex++;
             }else{ //up arrow 38
                 tabindex--;
             }
             $('[tabindex=' + tabindex + ']').focus();
         }
     });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM