jQuery keypress()事件获取文本 [英] jquery keypress() event get text
问题描述
我希望在文本框上发生按键操作时运行一个函数,所以我有以下代码:
I want a function to be run when a keypress occurs on a text box, so I have this code:
$("input[x]").keypress(function() {
DoX();
})
这很好,但是在我的函数中,我想根据文本框中的文本值来做某事
This is working fine, but in my function I want to do something based on the text value in the textbox
var textValue = ("input[x]").val();
现在这里的问题是它落后于键,因此如果我的文本框显示"He"并且我键入"l",那么我希望我的textValue是"Hel",但它会返回先前的值他",因为可能尚未将字符放入文本框中.
Now the problem here is that it lags behind by a key so if my text box says 'He' and I type an 'l', then I want my textValue to be 'Hel', but it is returning the previous value 'He' because presumably the character hasn't been put in the text box yet.
有没有办法让"Hel"脱离我的功能?
Is there a way of getting 'Hel' out of my function here?
谢谢:)
推荐答案
您可以尝试使用keyup事件:
You can try to use the keyup event:
$(selector).keyup(function() {
var textValue = $(this).val();
DoX();
});
这篇关于jQuery keypress()事件获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!