admin管理员组

文章数量:1403145

When the textarea is in active use (meaning, a user is typing keystores in this field), if the sequence of characters being typed matches a defined sequence like "aeu" then, I would like to set focus out of the textarea and click a button.

Note: I am using the Mousetrap plugin to handle keyboard shortcuts. .min.js

Demo: /

As you can see in the demo page, I have tried blur() and trigger('blur') but, I wasn't able to remove focus out of the textarea.

When the textarea is in active use (meaning, a user is typing keystores in this field), if the sequence of characters being typed matches a defined sequence like "aeu" then, I would like to set focus out of the textarea and click a button.

Note: I am using the Mousetrap plugin to handle keyboard shortcuts. https://github./ccampbell/mousetrap/blob/master/mousetrap.min.js

Demo: http://jsfiddle/PqXWJ/5/

As you can see in the demo page, I have tried blur() and trigger('blur') but, I wasn't able to remove focus out of the textarea.

Share Improve this question edited Jun 17, 2013 at 20:22 Srikanth AD asked Jun 17, 2013 at 20:14 Srikanth ADSrikanth AD 1,8642 gold badges14 silver badges19 bronze badges 5
  • blur should get the job done. What's happening is the mouseTrap plugin isn't catching the keystrokes. – Kevin B Commented Jun 17, 2013 at 20:16
  • The mousetrap plugin works fine when the focus is not on the textarea. – Srikanth AD Commented Jun 17, 2013 at 20:18
  • Right. while focus is on the textarea, keypress keyup and keydown events aren't reaching the document which is a requirement for the Mousetrap plugin to function. – Kevin B Commented Jun 17, 2013 at 20:18
  • So, do you have any suggestions? also, I can skip the mousetrap plugin if I can handle - detecting a bination of keystores by other means. – Srikanth AD Commented Jun 17, 2013 at 20:20
  • Sorry, i was wrong. There's a bug somewhere in the mousetrap that's causing it to reset the current sequence on each keypress when in a textarea. I would just not use the plugin and roll my own. jsfiddle/PqXWJ/18 – Kevin B Commented Jun 17, 2013 at 20:43
Add a ment  | 

4 Answers 4

Reset to default 4

I am not sure what that library you are using is but I got the functionality you wanted using some simple jQuery event binding.

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

Here is an example in jsFiddle.

UPDATE:

Here is a reworked example using a text area and allowing for any number of characters to be entered before the desired string. Example

UPDATE 2:

Added the button functionality you requested. Cheers!

jsFiddle

You can do that without plugin -

$('textarea').on('keyup', function () {
    if (this.value === 'enter') {
        $(this).blur();
    }
});

Demo ----> http://jsfiddle/PqXWJ/5/

I think this is what you might be looking for

<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/ZYJxq/4/

The mousetrap plugin you are using seems to have a bug with textareas, though since this is such an easy task, it would be quicker to roll my own than fix the broken plugin.

http://jsfiddle/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 = [];
    }
});

本文标签: javascriptHow to remove focus from textarea when it is in active useStack Overflow