多个文本框录入,使用回车键替找Tab键

本文介绍了一种在HTML页面中实现自动聚焦首个文本框,并通过键盘回车键在多个文本框间进行切换的方法。该方法利用了jQuery来绑定键盘事件,实现了无需鼠标操作即可快速填写表单的功能。

为了快速把form的所有文框输入完毕,我们不必使用鼠标去focus文本框。

在html页中放几个文本框:

 

<div class="DivInput">
    <div>
        <input id="Text1" type="text" />
    </div>
    <div>
        <input id="Text2" type="text" />
    </div>
    <div>
        <input id="Text3" type="text" />
    </div>
    <div>
        <input id="Text4" type="text" />
    </div>
    <div>
        <input id="Text5" type="text" />
    </div>
    <div>
        <input id="Text6" type="text" />
    </div>
</div>
Source Code

 

有几点需要注意和了解。
网页加载时,focus第一个文本框,
还要监控回车值:

 

 var $txtInput = $('input:text');

        $txtInput.first().focus();

        $txtInput.bind('keydown', function (e) {
            if (e.which == 13) {
                e.preventDefault();

                var nxtIdx = $txtInput.index(this) + 1;              
                $txtInput.filter(":eq(" + nxtIdx + ")").focus();
            }
        });
Souce Code

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值