div的Onclick/Javascript; Rails迭代器 [英] Onclick/Javascript for div; Rails iterator

查看:90
本文介绍了div的Onclick/Javascript; Rails迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录页面,列表显示在索引页面上.

I have a directory page where the listings are rendered on the index page.

<% @listings.each do |listing| %>
  # do some stuff
<% end %>

我已经在每个列表中添加了一个数据切换按钮-基本上是一个按钮.

I've added a data-toggle to each listing - basically a button.

 <a id="chat-menu-toggle" href="#dr" class="chat-menu-toggle" >
   <div class="iconset top-chat-dark ">
      <span class="badge badge-important hide" id="chat-message-count">1
      </span>
   </div>                 
  </a>

此数据切换打开STATIC格. div是带有内容的滑块.

This data-toggle opens a STATIC div. The div is a slider with content.

<div id="dr" class="chat-window-wrapper">

我想使用每个按钮将listing.id传递给变量.然后,我可以在整个div中使用该ID.

I want to use each button to pass the listing.id to a variable. Then, I can use that ID throughout the div.

例如:

具有ID的列表:

  • 堆栈-1
  • 溢出-2
  • Ruby-3
  • 路轨-4
  • Onclick-5

假设我单击ID为3的"Ruby"按钮,我希望ID像这样传递.

Let's say I click the button for "Ruby" which has an id of 3, I want the id to be passed around like so.

 variable = 3

 <div id= <%= variable %> class="chat-window-wrapper">

推荐答案

不确定在整个div中使用该ID"是什么意思.但是您可以尝试一下,如果有帮助的话?

Not to sure what you mean by "use that ID throughout the div." but you could try this if it helps you?

<div data-id="<%=listing.id %>" class="chat-window-wrapper">

如果要从打印的红宝石中获取ID的值,则可以尝试添加事件侦听器

If you want to get the id's value from what ruby printed then you can try adding an event listener

$(".chat-window-wrapper").on("click",function() { 
    var x = parseInt($(this).attr('data-id');
}

要访问迭代器之外的列表,请尝试

To access listings outside iterator try

listings[index] 

@allListings = listings. Then access @allListings[index]

但就我生锈的红宝石知识而言,我认为只要控制器在上下文中将列表传递给视图,就应该可以访问列表.

but as far as my rusty ruby knowledge goes, i think listings should be accessible as long as the controller is passing it in the context to the view.

这篇关于div的Onclick/Javascript; Rails迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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