$(function () {
    $('input:text:first').focus();
    var $inp = $('input:text');
    $inp.bind('keydown', function (e) {
        var key = e.which;
        if (key == 13) {
            e.preventDefault();
            var nxtIdx = $inp.index(this) + 1;
            $(":input:text:eq(" + nxtIdx + ")").focus();
        }
    });
});上面这段代码是网上找的,部分符合需求,但我的页面中除了text还有radio、button、textarea、select等,请问怎么改写这段代码来实现页面的回车TAB顺序?TAB顺序

解决方案 »

  1.   

    直接用 Dom 属性 tabindex 就行了<input type="text" tabindex="1" >
    <input type="text" tabindex="2" >
     
      

  2.   


    $(function () {
        $('input:text:first').focus();
        
        var $inp = $('input,button,textarea,select');
        $inp.bind('keydown', function (e) {
            var key = e.which;
            if (key == 13) {
                e.preventDefault();
                var nxtIdx = $inp.index(this) + 1;
                $inp[(nxtIdx + 1) % $inp.length ].focus();
              
            }
        });
    });
      

  3.   

    jquery遍历的时候是按照html代码从上到下从左到右遍历的,如果你用样式浮动控制控件位置把本来代码在下面的硬是放到上面的话脚本里是不知道的。
    这段代码没多大问题,把text去掉就是了,关键是你的代码里控件的顺序是不是你想要的顺序
      

  4.   

    看了下代码,终于明白楼主说的(回车TAB顺序)。其实最好就是在需要(回车TAB顺序)的地方搞个标识(有顺序的标识)。
    再按标识focus.
    再说人家一般都用tab的,很小用回车模拟tab
      

  5.   

    你元素上有没有加  tabindex 没加按默认顺序上面那就行了,有就以tabindex排序一下就行了<!DOCTYPE html>
    <html lang="en">
      <body> 
        <input type="password"  tabindex=4/>
        <input type="checkbox" name="" value="" tabindex=1 />
        <input type="radio" name="" value="" tabindex=3 />
        <input type="button" tabindex=2 /><textarea></textarea> </body>
     
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
     <script>
    $(function () {
        $('input:text:first').focus();
        var $inp = $('input,button,textarea,select');
        $inp.sort(function(a,b){ return  (a.tabIndex||999) - (b.tabIndex||999)  });
        $inp.bind('keydown', function (e) {
            var key = e.which;
            if (key == 13) {
                e.preventDefault();
                var nxtIdx = $inp.index(this) + 1;
                $inp[(nxtIdx + 1) % $inp.length ].focus();
            }
        });
    });
    </script>
     </html>
      

  6.   

    支持这种写法,而且冒似本来就支持tab啊。只是顺序要控制就要用tabindex=来控制了