最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - On click of TAB the focus should be on the next textbox - Stack Overflow

programmeradmin0浏览0评论

I have two textboxes and when TAB is pressed from one textbox it goes to another place instead of to the next textbox.

My code is:

   $(function () {

        $("input[id$=txt1]").bind('keydown',
        function (e) {
            if (e.which == 9) {
                // alert("hello");
                $("input[id$=txt2]").focus();
            }
        }
        );

    });

When I press tab for txt1 it should go to txt2 but it doesn't.

Any help?

I have two textboxes and when TAB is pressed from one textbox it goes to another place instead of to the next textbox.

My code is:

   $(function () {

        $("input[id$=txt1]").bind('keydown',
        function (e) {
            if (e.which == 9) {
                // alert("hello");
                $("input[id$=txt2]").focus();
            }
        }
        );

    });

When I press tab for txt1 it should go to txt2 but it doesn't.

Any help?

Share Improve this question edited Apr 5, 2012 at 18:53 demBones 1551 silver badge5 bronze badges asked Jan 5, 2012 at 6:04 G.S BhangalG.S Bhangal 3,3004 gold badges26 silver badges48 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You don't have to do any code here. You just have to assign tab indexes in a proper order.

For example:

<input type="text" value="a" tabindex="1" />
<input type="text" value="c" tabindex="3" />
<input type="text" value="b" tabindex="2" />

If you set a focus to "a", then push TAB, focus will move to "c", and then to "b".

See demo here: http://jsbin./epacop/2

Just use preventDefault and it will work

$("#txt1").bind('keydown',
function (e) {
    if (e.which == 9) {
        $("#txt2").focus();
        e.preventDefault()
        }
    }
);

BTW, its much better to use direct selectors with ids (#txt2 instead of input[id$=txt2]).

发布评论

评论列表(0)

  1. 暂无评论