Javascript:由于类名中的 rails 代码而无法识别的表达式


$(".collapse.summary_row_<%= pin %>").each(function() {
      var thisId = $(this).find('#pin').text();
      var sumVal = parseFloat($(this).find('#price').text());
      var $rowsToGroup = $(this).nextAll('tr').filter(function() {
        return $(this).find('#pin').text() === thisId;
      });
      $rowsToGroup.each(function() {
        sumVal += parseFloat($(this).find('#price').text());
        $(this).remove();
      });
      $(this).find('#price').text(sumVal);
    });

<tr class="clickable" data-toggle="collapse" id="summary_row_<%=pin%>" data-
target=".summary_row_<%= pin %>">
<td><i class="glyphicon glyphicon-plus"></i></td>
<td> <%= pin%> </td>
<td align="right"> <strong> <%= number_to_currency(amount, unit: "", 
precision: 2) if amount %> </strong> </td>
</tr>
<tr id="child_row_<%=pin%>" class="collapse summary_row_<%= pin %>" 
align="right" >
  <td class="child_data_<%=pin%>"><strong>Year</strong></td>
  <td><strong>Quarter</strong></td>
  <td><strong>Amount</strong></td>
</tr>
<% if detail %>
<% detail.each do |key, value| %>
<tr id="child_row_<%=pin%>" class="collapse summary_row_<%= pin %>" 
bgcolor="#fba993" align="right" >
  <td class="child_data_<%=pin%>" id="pin"><%= value[0]%></td>
  <td id="quart"><%= value[1]%></td>
  <td id="price"><%= number_to_currency(value[2], unit: "", precision: 2) %>
</td>
</tr>
<% end %>
上面的代码是

Javascript代码,下面的代码是html/ruby代码。我正在尝试使它成为当单击.clickable tr类时,该函数将完成它的工作,但它没有,我收到以下消息:

Uncaught Error: Syntax error, unrecognized expression: .collapse.summary_row_<%= pin %>

我注意到我的 JS 文件中的其他函数可以工作,但它们的类名中没有 ruby 语法,所以我假设 ruby 代码与此错误有关。

你在 JavaScript 文件中编写 Rails 代码,它在那里不起作用,你必须在文件中编写 rails 代码html.erb或者你可以在html.erb文件中<script>标签中调用该函数,或者你可以在 html 标签中使用动态data attributes

如果你想

在Javascript中使用Ruby变量,你应该在你的JS文件中添加erb扩展名:my_file.js.erb

另一种选择是直接使用 my_file.coffee .在这里你可以改变适应你的JS文件 JsToCoffee

希望对您有所帮助,

最新更新