javascript - 按下/按键后获取输入值

标签 javascript jquery jquery-events

我有一个<input type="text"> ,并且我需要在文本框中的文本更改后调用一个函数(包括在 jQuery 的 keydown 和 keypress 处理程序期间执行的操作)。

如果我从 jQuery 处理程序调用我的函数,我会看到添加输入之前的值 ( e.target.value )。由于我不想每次都手动将输入添加到值上,因此如何调用我的函数并让它使用更新后的值?

最佳答案

如果我理解正确,那么你就可以这样做

$('input').bind('change keydown keyup',function (){
   /// do your thing here.
   //  use $(this).val() instead e.target.value
});

更新:2013 年 3 月 5 日

请注意:您最好使用 .on() 而不是 .bind()

As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document. For earlier versions, the .bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to .bind() occurs. For more flexible event binding, see the discussion of event delegation in .on() or .delegate().

了解更多信息jQuery Bind

关于javascript - 按下/按键后获取输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5141431/

相关文章:

jquery ajax 获得成功

jquery - 文本框值更改事件没有被触发 jQuery

Javascript/CSS3 转换问题

javascript - 使用多个 Canvas 比使用一个 Canvas 慢吗?

javascript - jQuery 迭代对象(JSON)

javascript - selecter.eventHandler(...some code...).eventHandler(...some code...) 在 jQuery 中做什么?

javascript - jquery警告框简单计算

javascript - SlickGrid 过滤多列(使用 JSFiddle)

javascript - jQuery插件绑定(bind)ctrl+mouseclick事件

javascript - 当我加载 .xml 文件时,它的内容有时会出现在我的网页上,但当我刷新时,它就会消失。 (javascript、jquery、ajax、xml)