2016-08-04 65 views
0

这可能是一个相当不成熟的Coffeescript问题:我想检索文本框元素的内容后改变。我试过以下内容:Coffeescript:如何检索元素内容后发生变化

$("#notes").change (e) -> 
    alert("Note content changed. Is now " + $("#notes").text()) 

其中“notes”是文本框的ID。

这不起作用。它始终显示“笔记”文本框的原始内容。我怀疑这是因为在加载时咖啡脚本正在编译为JavaScript。

我想用Coffeescript做什么?如果是这样,请给我看看。

谢谢。

回答

1

您以错误的方式从textarea中读取值。

没有jQuery的:

$("#notes").change (e) -> 
    alert("Note content changed. Is now #{ e.currentTarget.value }") 

https://jsfiddle.net/sr3tkaw7/3/

使用jQuery:

$("#notes").change (e) -> 
    alert("Note content changed. Is now #{ $(e.currentTarget).val() }") 

https://jsfiddle.net/sr3tkaw7/4/

+0

谢谢!那就是诀窍。 –

相关问题