Rails 4:选择具有动态生成ID的DOM元素 [英] Rails 4: select DOM element with dynamically generated id

查看:162
本文介绍了Rails 4:选择具有动态生成ID的DOM元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails 4应用中,我对我的一个观点有以下DOM结构:

 < tr id = post_row_<%= post.id%>> 
[...]#截断为brivety
< td class =cell_content_center post_approval_section>
[...]#截断为brivety
< / td>
< / tr>

我需要更新 td的内容使用JavaScript。



为了选择它,我尝试:

  $('tr#post_row_<%= j post.id%>> td.post_approval_section')。html('<%= j render(partial:calendars / post_approval)%>'); 

但这会给我一个错误:

 无法加载资源:服务器响应状态为500(内部服务器错误)

如您所见,父代 tr id 是动态生成的。 p>

如何用JavaScript选择它?

解决方案

使用双引号)而不是单引号(''),如: -

  $(tr#post_row_<%= j post。 id%>> td.post_approval_section)


In my Rails 4 app, I have the following DOM structure on one of my views:

<tr id="post_row_<%= post.id%>">
  [...] # Truncated for brivety
  <td class="cell_content_center post_approval_section">
    [...] # Truncated for brivety
  </td>
</tr>

I need to update the content of the td with JavaScript.

In order to select it, I tried:

$('tr#post_row_<%= j post.id %> > td.post_approval_section').html('<%= j render(partial: "calendars/post_approval") %>');

but this gives me an error:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

As you can see, the id of the parent tr is dynamically generated.

How can I select it with JavaScript?

解决方案

Use double quotation(" ") instead of single quotes(' ') like:-

$("tr#post_row_<%= j post.id %> > td.post_approval_section")

这篇关于Rails 4:选择具有动态生成ID的DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆