javascript - 未从文本框获取最新值

标签 javascript jquery textbox coffeescript

我正在尝试在coffescript/jquery中实现以下内容

-> 当用户输入时,检查文本框是否为空(或有文本“写评论...”),隐藏“添加评论”按钮。否则显示“添加评论”按钮

enter image description here

enter image description here

但是,我面临这样的问题:从文本框返回给我的 val() 总是落后一次击键。

例如,如果我在框中输入“sad”,则 target.val() 仅返回“sa”。

enter image description here

如何在输入时获取文本框中的最新值?

我的实现如下

events:
  "keydown .comment_area": "commenting"

commenting: (e) ->
  target = @$(e.currentTarget)

  //target.val() is not returning me the latest character entered in the box
  if $.trim(target.val()) == "" or $.trim(target.val()) =="Write a comment..."
    //hide the 'add' button
    @$('.add_comment').hide()
  else
    //show the 'add' button
    @$('.add_comment').show()

最佳答案

绑定(bind)到 keyup 事件而不是 keydown

请参阅keyboard event order .

关于javascript - 未从文本框获取最新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8350016/

相关文章:

jquery - 如何将文本输入 value() 添加到 JSON 数组中

silverlight - 如何绑定(bind)文本框的 SelectionStart 属性?

javascript - 使用 Javascript 将两个数字相加并在文本框中显示结果

javascript - 第二次调用 javascript click() 方法不起作用

javascript - 复杂的数学水平视差缓动函数

jquery - 根据 jQuery 中的数据属性过滤项目

c# - 在选项卡控件内按名称顺序迭代文本框

javascript - 这个循环和返回数据的更好解决方案? Javascript

javascript - PHP防止用户粘贴无效字符

javascript - 单击新窗口中的按钮后执行函数