基于输入键的 Tabindex 通过获取表单元素在 Jquery 中不起作用

发布于 2024-10-10 15:46:51 字数 839 浏览 0 评论 0原文

我有一个表单,我可以通过 serializeArray() 获取所有表单元素。 我想使用 Enter 键根据 tabindex 值focus() 关注表单元素。只有当它有价值时,否则就专注于它本身。

对 jQuery 来说不太新鲜,所以如果有任何错误......

$.fn.entertab = function()
  {

 var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
  var maxTabIndex = 20;
        var elements = this.serializeArray();
     $.each(elements, function(i, element)
        {

 this.keypress(function(e){ 
    var nTabIndex=this.tabIndex;
       var myNode=this.nodeName.toLowerCase();
            if(nTabIndex > 0 && key == 13 && nTabIndex <= maxTabIndex && ((!myNode.attr("disabled")) || (myNode.val == "")))
      {
   myNode.focus(); 
   }
   else
   {
   nTabIndex=this.tabIndex+1;
      myNode.focus();
   }
   });

     });
        }
  $("theform").entertab();

I have a form, and I am able to get all form elements through serializeArray().
I want to focus() on form elements based on their tabindex value using enter key. Only if it has value in it or else focus on itself.

Little new to jQuery so if any mistakes...

$.fn.entertab = function()
  {

 var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
  var maxTabIndex = 20;
        var elements = this.serializeArray();
     $.each(elements, function(i, element)
        {

 this.keypress(function(e){ 
    var nTabIndex=this.tabIndex;
       var myNode=this.nodeName.toLowerCase();
            if(nTabIndex > 0 && key == 13 && nTabIndex <= maxTabIndex && ((!myNode.attr("disabled")) || (myNode.val == "")))
      {
   myNode.focus(); 
   }
   else
   {
   nTabIndex=this.tabIndex+1;
      myNode.focus();
   }
   });

     });
        }
  $("theform").entertab();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生寂 2024-10-17 15:46:51

你也可以尝试这个
HTML

<input id="122" class='TabOnEnter' tabindex="1" /><br>
<input id="123" class='TabOnEnter' tabindex="2" /><br>
<input id="124" class='TabOnEnter' tabindex="4" />This input is hidden<br>
<input id="124" class='TabOnEnter' tabindex="5" /><br>
<input id="125" class='TabOnEnter' tabindex="3" /><br>
<textarea class='TabOnEnter' tabindex="6">Hi, I am a test area</textarea>

脚本
///////////

$(document).on("keypress", ".TabOnEnter" , function(e)
  {
    //Only do something when the user presses enter
    if( e.keyCode ==  13 )
    {
       var nextElement = $('[tabindex="' + (this.tabIndex+1)  + '"]');
       console.log( this , nextElement ); 
       if(nextElement.length )
         nextElement.focus()
       else
         $('[tabindex="1"]').focus();  
    }   
  });

//Hidden inputs should get their tabindex fixed, not in scope ;)
//$(function(){ $('input[tabindex="4"]').fadeOut();  })

//////////////////////////////
在 EI、Chrome、Mozilla 中运行良好。未在 safari 和其他浏览器中测试

You can also try this
HTML

<input id="122" class='TabOnEnter' tabindex="1" /><br>
<input id="123" class='TabOnEnter' tabindex="2" /><br>
<input id="124" class='TabOnEnter' tabindex="4" />This input is hidden<br>
<input id="124" class='TabOnEnter' tabindex="5" /><br>
<input id="125" class='TabOnEnter' tabindex="3" /><br>
<textarea class='TabOnEnter' tabindex="6">Hi, I am a test area</textarea>

SCRIPT
///////////

$(document).on("keypress", ".TabOnEnter" , function(e)
  {
    //Only do something when the user presses enter
    if( e.keyCode ==  13 )
    {
       var nextElement = $('[tabindex="' + (this.tabIndex+1)  + '"]');
       console.log( this , nextElement ); 
       if(nextElement.length )
         nextElement.focus()
       else
         $('[tabindex="1"]').focus();  
    }   
  });

//Hidden inputs should get their tabindex fixed, not in scope ;)
//$(function(){ $('input[tabindex="4"]').fadeOut();  })

//////////////
Worked Fine in EI,Chrome, Mozilla . not tested in safari and other browser

孤独陪着我 2024-10-17 15:46:51

我想我明白你想做什么。我重写了你的代码并最终得到了这个:

(function($){

    $.fn.entertab = function(options) {

        var defaults = {
            maxTabIndex: 20
        };

        options = $.extend({}, defaults, options);

        return this.filter('form').each(function(){

            var $this = $(this),
                $elms = $this.find("[tabindex]");

            $elms.each(function(){
                var $elm = $(this),
                    idx = parseInt($elm.attr("tabindex"));
                if (idx > options.maxTabIndex) {
                    return;
                }
                $elm.keydown(function(e){
                    if (e.which == 13 && ($elm.val() != '')) {
                        $elms.filter("[tabindex="+(idx+1)+"]").focus();
                        e.preventDefault();
                    }
                });
            });
        });
    };

})(jQuery);

jsFiddle 上有一个工作示例。

I think I understand what you want to do. I rewrote your code and ended up with this:

(function($){

    $.fn.entertab = function(options) {

        var defaults = {
            maxTabIndex: 20
        };

        options = $.extend({}, defaults, options);

        return this.filter('form').each(function(){

            var $this = $(this),
                $elms = $this.find("[tabindex]");

            $elms.each(function(){
                var $elm = $(this),
                    idx = parseInt($elm.attr("tabindex"));
                if (idx > options.maxTabIndex) {
                    return;
                }
                $elm.keydown(function(e){
                    if (e.which == 13 && ($elm.val() != '')) {
                        $elms.filter("[tabindex="+(idx+1)+"]").focus();
                        e.preventDefault();
                    }
                });
            });
        });
    };

})(jQuery);

There's a working example on jsFiddle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文