Javascript:如何将最后两个字符输入textarea?

时间:2022-08-11 17:06:02

What is the best way to grab the last two characters typed into a textarea box?

抓取输入textarea框的最后两个字符的最佳方法是什么?

I need the last 2 characters typed NOT the last two characters of the overall string.

我需要输入最后2个字符而不是整个字符串的最后两个字符。

Appreciate the help!

感谢帮助!

2 个解决方案

#1


2  

Here's a demo of doing it with jQuery, while displaying the last two characters that were entered: http://jsfiddle.net/Ender/5gUHb/

这是一个用jQuery做的演示,同时显示输入的最后两个字符:http://jsfiddle.net/Ender/5gUHb/

And the source:

来源:

var keys = [];
$('#target').keypress(function(e) {
    keys.unshift(e.which);
    update();
});

function update() {
       $('#last')
           .prepend($('<li/>').text(String.fromCharCode(keys[0])))
           .children(':eq(2)').remove();
}

This demo captures the keypress event on the text area, unshifts the value onto the array (because then the indexes are representative of the number of keys that have been pressed since that key was pressed) and then updates the display.

此演示捕获文本区域上的按键事件,将值移至数组上(因为索引表示自按下该键后已按下的键数),然后更新显示。

The display update simply pushes the first value from the array to the top of a <ul> and removes the child (if any) at index 2 in the list.

显示更新只是将数组中的第一个值推送到

    的顶部,并删除列表中索引2处的子项(如果有)。

Note additionally that because of the way jQuery deals with .keypress(), you do NOT have to filter out modifier or arrow keys.

另外请注意,由于jQuery处理.keypress()的方式,您不必过滤掉修饰符或箭头键。

UPDATE Please see Tim Down's comment to my answer for an explanation of what filtering should take place. My initial note was mistaken, based on a quick test in Chrome.

更新请参阅Tim Down对我的回答的评论,以解释应该进行哪些过滤。基于Chrome中的快速测试,我最初的注意事项是错误的。

#2


6  

You need to catch the keypress event on the textarea and then keep a log of keys that were pressed. Note that this is going to catch arrow keys, shift, alt, etc so if you just want characters you need to filter them out.

您需要捕获textarea上的按键事件,然后记录按下的键。请注意,这将捕获箭头键,shift,alt等,所以如果你只想要字符,你需要将它们过滤掉。

Simple example:

var keyPresses = [];
textarea.onkeypress = function(ev){
   ev = ev || window.event;

   var key = ev.keyCode || ev.which;
   keyPresses.push(key);
}

#1


2  

Here's a demo of doing it with jQuery, while displaying the last two characters that were entered: http://jsfiddle.net/Ender/5gUHb/

这是一个用jQuery做的演示,同时显示输入的最后两个字符:http://jsfiddle.net/Ender/5gUHb/

And the source:

来源:

var keys = [];
$('#target').keypress(function(e) {
    keys.unshift(e.which);
    update();
});

function update() {
       $('#last')
           .prepend($('<li/>').text(String.fromCharCode(keys[0])))
           .children(':eq(2)').remove();
}

This demo captures the keypress event on the text area, unshifts the value onto the array (because then the indexes are representative of the number of keys that have been pressed since that key was pressed) and then updates the display.

此演示捕获文本区域上的按键事件,将值移至数组上(因为索引表示自按下该键后已按下的键数),然后更新显示。

The display update simply pushes the first value from the array to the top of a <ul> and removes the child (if any) at index 2 in the list.

显示更新只是将数组中的第一个值推送到

    的顶部,并删除列表中索引2处的子项(如果有)。

Note additionally that because of the way jQuery deals with .keypress(), you do NOT have to filter out modifier or arrow keys.

另外请注意,由于jQuery处理.keypress()的方式,您不必过滤掉修饰符或箭头键。

UPDATE Please see Tim Down's comment to my answer for an explanation of what filtering should take place. My initial note was mistaken, based on a quick test in Chrome.

更新请参阅Tim Down对我的回答的评论,以解释应该进行哪些过滤。基于Chrome中的快速测试,我最初的注意事项是错误的。

#2


6  

You need to catch the keypress event on the textarea and then keep a log of keys that were pressed. Note that this is going to catch arrow keys, shift, alt, etc so if you just want characters you need to filter them out.

您需要捕获textarea上的按键事件,然后记录按下的键。请注意,这将捕获箭头键,shift,alt等,所以如果你只想要字符,你需要将它们过滤掉。

Simple example:

var keyPresses = [];
textarea.onkeypress = function(ev){
   ev = ev || window.event;

   var key = ev.keyCode || ev.which;
   keyPresses.push(key);
}