【发布时间】:2016-03-06 01:46:16
【问题描述】:
在我的 Rails 应用程序中,我有一个 Post 模型。
我正在使用 coffeescript 使帖子表单更具动态性。
在posts.coffee,我有:
$(document).ready ->
text_max = 140
$('#character_count').html text_max + ' characters remaining'
$('#post_short_copy').keyup ->
text_length = $('#post_short_copy').val().length
text_remaining = text_max - text_length
$('#character_count').html text_remaining + ' characters remaining'
return
return
这真的很好用。
现在,我需要在这段代码中插入一个 if 语句,只执行$('#character_count').html text_max + ' characters remaining' if $('#post_short_copy').is(':empty')。
我已经尝试了以下代码:
$(document).ready ->
text_max = 140
if $('#post_short_copy').is(':empty') {
$('#character_count').html text_max + ' characters remaining'
}
$('#post_short_copy').keyup ->
text_length = $('#post_short_copy').val().length
text_remaining = text_max - text_length
$('#character_count').html text_remaining + ' characters remaining'
return
return
但这会返回错误:
SyntaxError: [stdin]:7:3: unexpected if
知道如何在 coffeescript 中正确实现 if 语句吗?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 if-statement coffeescript conditional-statements