在jQuery中使用ERB内部脚本标记
我正在尝试在Rails应用程序中执行一些相当简单的jQuery内部在jQuery中使用ERB内部脚本标记,jquery,ruby-on-rails,Jquery,Ruby On Rails,我正在尝试在Rails应用程序中执行一些相当简单的jQuery内部script标记(我知道,开发人员不好,没有cookie,不使用脚本标记),但在erb生成的ID方面遇到了问题。我认为: <% @clients_with_pricing.each do |client| %> <h3 id="client-<%= client.id %>-header"><%= client.first_name %> &l
script
标记(我知道,开发人员不好,没有cookie,不使用脚本标记),但在erb生成的ID方面遇到了问题。我认为:
<% @clients_with_pricing.each do |client| %>
<h3 id="client-<%= client.id %>-header"><%= client.first_name %> <%= client.last_name %></h3>
<div id="client-<%= client.id %>-div">
<h4><%= client.company_name %></h4>
<ul>
<% client.pricings.each do |pricing| %>
<li><%= number_to_currency(pricing.price, precision: 0) %>: <%= pricing.product.name %></li>
<% end %>
</ul>
</div> <!-- client id div -->
<% end %>
- :
以及以下jQuery:
<script type="text/javascript">
$(document).ready(function() {
$('.client-<&= client.id %>-header').click(function(){
$('.client-<&= client.id %>-div').toggleClass('hidden');
});
});
</script>
$(文档).ready(函数(){
$('.client--header')。单击(函数(){
$('.client--div').toggleClass('hidden');
});
});
当我查看呈现的HTML时,会正确地生成ID,但jQuery中仍然包含erb。谁能告诉我我做错了什么吗?ERB的正确语法是
ERB的正确语法是var name=“”代码>var name=“”代码>好的,我修复了明显的打字错误,现在得到了一个控制台错误:jquery.self-bd7ddd3….js?body=1:1503未捕获错误:语法错误,无法识别的表达式:.client--header
Hmm,这段代码是在您的erb文件中内联的,还是在单独的javascript文件中?它在我的html.erb文件中内联的。好的,我修复了明显的打字错误,现在得到了一个控制台错误:jquery.self-bd7ddd3….js?body=1:1503未捕获错误:语法错误,无法识别的表达式:.client--header
Hmm,这段代码是内嵌在您的erb文件中,还是在单独的javascript文件中?它内嵌在我的html.erb文件中。