如何从文本区移除焦点时,它是在积极使用

How to remove focus from textarea when it is in active use

本文关键字:文本区 焦点      更新时间:2023-09-26

当文本区域处于活跃使用状态时(意思是,用户在此字段中键入密钥库),如果键入的字符序列与定义的序列(如"aeu")相匹配,那么我想将焦点设置在文本区域之外并单击按钮

注意:我正在使用Mousetrap插件来处理键盘快捷键。https://github.com/ccampbell/mousetrap/blob/master/mousetrap.min.js

: http://jsfiddle.net/PqXWJ/5/

正如您在演示页面中看到的,我尝试了blur()trigger('blur'),但是,我无法将焦点从文本区域中移除。

我不确定你使用的库是什么,但我得到了你想要的功能使用一些简单的jQuery事件绑定。

$('#text').keyup(function () {
    if ($(this).val() === 'aeu') $(this).blur();
    $('#myBtnId').click();
});

下面是jsFiddle中的一个例子。

更新:

下面是一个重新设计的示例,使用文本区域,并允许在所需字符串之前输入任意数量的字符。示例

更新2:

添加了您所要求的按钮功能。干杯!

jsFiddle

不用插件也可以做到-

$('textarea').on('keyup', function () {
    if (this.value === 'enter') {
        $(this).blur();
    }
});
演示----> http://jsfiddle.net/PqXWJ/5/

我想这就是你要找的

<textarea id="upcinput"></textarea>
<p>Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will un <a id="exception-btn">click here</a></p>
$('#upcinput').keyup(function () {
 if ($(this).val() === 'lorem ipsum') { 
    $(this).blur();
    $('#exception-btn').click();
 }
});
http://jsfiddle.net/ZYJxq/4/

你正在使用的mousetrap插件似乎有一个文本区域的错误,虽然因为这是一个很容易的任务,它会更快地滚动我自己比修复损坏的插件。

http://jsfiddle.net/PqXWJ/18/

var sequence = [], targetsequence = [65,69,85,13];
$(document).on("keyup",function(e) {
    if (e.which == targetsequence[sequence.length]) {
        sequence.push(e.which);
        if (sequence.length == targetsequence.length) {
            alert("Correct Sequence!");
            $("#upcinput").blur();
            sequence = [];
        }
    }
    else {
        sequence = [];
    }
});