0
在我的Rails应用程序中,我有一个Post
模型。Rails 4:条件如果语句与咖啡脚本不工作
我正在使用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语句,以便只在$('#post_short_copy').is(':empty')
时执行$('#character_count').html text_max + ' characters remaining'
。
我曾尝试下面的代码:
$(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语句?
你是对的。只需用两个空格替换标签,它就可以工作。非常感谢。 –