Coffeescript:如何检索元素内容后发生变化
问题描述:
这可能是一个相当不成熟的Coffeescript问题:我想检索文本框元素的内容后改变。我试过以下内容:Coffeescript:如何检索元素内容后发生变化
$("#notes").change (e) ->
alert("Note content changed. Is now " + $("#notes").text())
其中“notes”是文本框的ID。
这不起作用。它始终显示“笔记”文本框的原始内容。我怀疑这是因为在加载时咖啡脚本正在编译为JavaScript。
我想用Coffeescript做什么?如果是这样,请给我看看。
谢谢。
答
您以错误的方式从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() }")
谢谢!那就是诀窍。 –