简体   繁体   English

用户按下Enter键时如何触发标签页

[英]How to trigger a Tab when user presses Enter

I got the code from here jquery how to catch enter key and change event to tab 我从这里得到的代码jQuery如何捕获Enter键并将事件更改为Tab

(function($) {
    $.fn.enterAsTab = function(options) {
        var settings = $.extend({
            'allowSubmit': false
        }, options);
        this.find('input, select, textarea, button').live("keypress", {localSettings: settings}, function(event) {
            if (settings.allowSubmit) {
                var type = $(this).attr("type");
                if (type == "submit") {
                    return true;
                }
            }
            if (event.keyCode == 13) {
                var inputs = $(this).parents("form").eq(0).find(":input:visible:not(disabled):not([readonly])");
                var idx = inputs.index(this);
                if (idx == inputs.length - 1) {
                    idx = -1;
                } else {
                    inputs[idx + 1].focus(); // handles submit buttons
                }
                try {
                    inputs[idx + 1].select();
                }
                catch (err) {
                    // handle objects not offering select
                }
                return false;
            }
        });
        return this;
    };
})(jQuery);

My questions are: 我的问题是:

  1. I want this script also work on select (dropdown), textarea, button[not with type=submit]. 我希望此脚本也可以用于选择(下拉),文本区域,按钮[不带类型=提交]。 It is working great on textarea, and buttons[not with type=submit] but it is not working with select(dropdown). 它在textarea上非常有用,并且在button [not with type = submit]上很好用,但是在select(dropdown)上不起作用。

  2. I found out that this script failed to move to next input when passing a disabled input. 我发现传递禁用的输入时,该脚本无法移至下一个输入。 For example I have three input, qty, qtyConv and memo. 例如,我有三个输入,qty,qtyConv和memo。 all of them are textfields, but qtyConv is disabled. 它们都是文本字段,但是qtyConv被禁用。 When I am in qty and I press enter, I could not move to memo. 当我处于数量状态并且按Enter键时,我无法移动到备忘录。

Any help to improve this script to meet my requirements? 对改进此脚本以满足我的要求有帮助吗?

Thank you in advance. 先感谢您。

Daniel 丹尼尔

Solution: use keydown instead of keypress as suggested by Nitish Dhar. 解决方案:使用keydown代替Nitish Dhar建议的按键。

Couple of things - 几件事-

  1. Selector being used is wrong. 使用的选择器错误。 The selector defined is wrong in terms of looking for the not disabled ones, use this one instead - 在寻找未禁用的选择器方面,定义的选择器是错误的,请改用此选择器-

     $(this).parents("form").eq(0).find(":input:visible:not(:disabled):not([readonly])"); 
  2. Firefox 29.0 has a bug with keypress on select inputs, use keydown instead - Select is not working for you in firefox due to a bug (or not a bug) where they don't listen to keypress when its on a select input - https://support.mozilla.org/en-US/questions/998291 . Firefox 29.0 在选择输入时存在按键输入错误 ,请改用keydown-由于错误(或不是错误),在Firefox中Select不适用于您,因为在输入选择输入时他们不收听按键输入-https: //support.mozilla.org/zh-CN/questions/998291

WORKING DEMO - http://codepen.io/nitishdhar/pen/Gxbhm (works in chrome & FF as well) 工作演示-http : //codepen.io/nitishdhar/pen/Gxbhm (也适用于chrome和FF)

Complete Code 完整的代码

(function($) {
    $.fn.enterAsTab = function(options) {
        var settings = $.extend({
            'allowSubmit': false
        }, options);
        $(this).find('input, select, textarea, button').live("keydown", {localSettings: settings}, function(event) {
            if (settings.allowSubmit) {
                var type = $(this).attr("type");
                if (type == "submit") {
                    return true;
                }
            }
            if (event.keyCode == 13) {
                var inputs = $(this).parents("form").eq(0).find(":input:visible:not(:disabled):not([readonly])");
                var idx = inputs.index(this);
                if (idx == inputs.length - 1) {
                    idx = -1;
                } else {
                    inputs[idx + 1].focus(); // handles submit buttons
                }
                try {
                    inputs[idx + 1].select();
                }
                catch (err) {
                    // handle objects not offering select
                }
                return false;
            }
        });
        return this;
    };
})(jQuery);

$("#form").enterAsTab({ 'allowSubmit': true});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM