【问题标题】:Coffeescript unexpected terminator errorCoffeescript 意外终止错误
【发布时间】:2013-04-16 04:25:03
【问题描述】:

我的 /assets/javascripts/leads.js.coffee 中有这个

jQuery ->
  getRowColour = (status) ->
    switch status
    when "rejected" then return '#FFA500'
    when "confirmed" then return '#C0C0C0'
    when "didn't connect" then return '#90EE90'
    else return '#FFFFFF'

这在我的 /views/leads/index.html.erb

<%= f.select(:status, ["to call","didn't connect","confirmed","rejected"], {:selected => lead.status}, :onchange => "$('#lead_form_#{lead.id}').submit();document.getElementById('lead_row_#{lead.id}').style.backgroundColor=getRowColour(#{lead.status});") %>
        <% end %> 

可以看出我在 f.select 中的 onchange 函数有一个 javascript 调用我的咖啡脚本文件中的函数。

请告诉我哪里出错了?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 coffeescript


    【解决方案1】:

    whenelse 语句需要比 switch 多缩进一级。

    jQuery ->
      getRowColour = (status) ->
        switch status
          when "rejected" then return '#FFA500'
          when "confirmed" then return '#C0C0C0'
          when "didn't connect" then return '#90EE90'
          else return '#FFFFFF'
    

    另外,switch 是 CoffeeScript 中的表达式,也是函数中的最后一个表达式,您无需在 when 之后添加 return

    【讨论】:

    • 哦,谢谢。这行得通。但是还有另一件事,颜色不会改变,浏览器调试器会抛出错误:getRowColour undefined。你能帮帮我吗?
    • @sleeping_dragon,我已经在这里回答了这个问题 - stackoverflow.com/questions/6089992/… 如果有帮助,请告诉我!
    • 这确实有效。谢谢。但是现在,AJAX 不起作用。浏览器调试器没有显示任何错误,但它也没有改变行颜色!
    • @sleeping_dragon,嗯,你可能最好问另一个问题,以及相关的 JS、生成的 html 等。如果你在这里发布链接,我会尽力提供帮助可以。
    • 在这里提出了一个新问题:stackoverflow.com/questions/16161850/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多