简体   繁体   English

jQuery获取带有Rails的元素类在Firefox中引发错误

[英]Jquery get element class with rails throws error in Firefox

I use the following JQuery script to update a specific element when the user clicks on a button: 当用户单击按钮时,我使用以下JQuery脚本更新特定元素:

$(function(){
 $("#item_<%= @detail.itemID %>").addClass('selected').html("<%= escape_javascript(render 'pages/item', :item => @detail.item, :@order => @detail.order) %>");
)};

Here is the relevant element in my view: 这是我认为的相关元素:

<tr class = "item_row">
<% items.each do |item| %>
<td class = "item_data <%= item_select(item)%>", id = "item_<%= item.itemID %>" >
    <%= render 'pages/item', :item => item %>
</td>
<% end %>

I am working with Rails and JQuery. 我正在使用Rails和JQuery。 The script updates properly with Chrome, but with Firefox the following error gets thrown (I debugged with Firebug): 该脚本可使用Chrome正确更新,但使用Firefox会引发以下错误(我使用Firebug进行了调试):

Error: Syntax error, unrecognized expression: #item_<%= @detail.itemID %>

I use this rails bit of code because I want update only the specific item that relates to what the user clicked. 我使用这段代码是因为我只想更新与用户单击的内容相关的特定项目。 Clearly I need to adapt my JQuery so Firefox can handle the rails element of my code properly, but can anyone point me in the right direction? 显然,我需要调整我的JQuery,以便Firefox可以正确处理代码的rails元素,但是有人可以指出正确的方向吗?

I'm assuming that that javascript code is inside an asset file. 我假设javascript代码位于资产文件中。 You can do this but if you set the file to use erb by adding a .erb to the file name. 您可以执行此操作,但是如果您通过在文件名中添加.erb来将文件设置为使用erb,则可以执行此操作。 But in your case, you want to evaluate a ruby expression that uses an instance variable which can only be done when you have access to the variable. 但是在您的情况下,您想评估使用实例变量的ruby表达式,只有在您有权访问该变量时才能执行该实例变量。 If you move that javascript function inside the view, you should be able to run it without any errors. 如果您在视图内移动该javascript函数,则应该可以运行它而不会出现任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM