我正在尝试在coffescript/jquery中实现以下内容
-> 当用户输入时,检查文本框是否为空(或有文本“写评论...”),隐藏“添加评论”按钮。否则显示“添加评论”按钮
但是,我面临这样的问题:从文本框返回给我的 val() 总是落后一次击键。
例如,如果我在框中输入“sad”,则 target.val() 仅返回“sa”。
如何在输入时获取文本框中的最新值?
我的实现如下
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/