Tabindex基于输入键,通过获取表单元素不在Jquery中工作

我有一个表单,我可以通过serializeArray()获取所有表单元素。 我想使用回车键基于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(); 

您也可以尝试使用此HTML

 

This input is hidden


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

 $(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和其他浏览器中测试过

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

 (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的工作示例。