简体   繁体   English

jQuery + Rails +切换图像

[英]jQuery + Rails + Toggle on Image

I have a list of checkboxes with associated images and titles in as labels. 我有一个复选框列表,其中包含相关图像和标题作为标签。 I run an each loop to pull the data for the checkboxes. 我运行each循环来拉取复选框的数据。 I simply want the images to be highlighted when onclick. 我只想在onc​​lick时突出显示图像。 For some reason, the jQuery toggle script I'm using doesn't work inside the loop. 出于某种原因,我正在使用的jQuery切换脚本在循环中不起作用。

js:

    <script>
      $("img.choice").click(function() {
        $(this).toggleClass("new").toggleClass("new1");
          });
    </script>

css:

    .new {
      padding-top: 0px;
      padding-bottom: 0px;
      margin-top: 0px;
      margin-bottom: 0px;
      box-shadow: 2px 2px 4px #888888;

    }

    .new:hover {
        box-shadow: 2px 2px 2px #001f36;
        border-color: #FF804D;
    }

    .new:active {
        box-shadow: 2px 2px 2px #001f36;
        border-color: #FF804D;
    }

    .new1 {
      border: outset;
      border-color: #FF804D;
    }


html:

    <div class="row" align='center'>                    
        <% Category.select { |category| category.gender == 'girl' }.each do |category| %>
            <div class="span4" align="center" style="padding-top:15px">
                <label>
                    <div style="padding-bottom:5px">
                        <h13><%= category.name.capitalize %></h13>
                    </div>
                    <img id="choice-<%= category.id -%>" src= <%= category.image %> class="choice new" />
                    <div>
                        <%= check_box_tag 'category_ids[]', category.id %>
                    </div>
                </label>    
            </div>
        <% end %>
    </div>

Here's a JSFiddle: 这是一个JSFiddle:

http://jsfiddle.net/misham/xu6Kf/ http://jsfiddle.net/misham/xu6Kf/

you need to wrap the JS code in a $(document).ready, so the events get registered: 你需要将JS代码包装在$(document).ready中,以便事件得到注册:

<script>
  $(document).ready(function(){
     $("img.choice").click(function() {
       $(this).toggleClass("new").toggleClass("new1");
  });
  }) ;
</script>

Ideally, you should place the JS code in the appropriate file in assets: 理想情况下,您应该将JS代码放在资产中的相应文件中:

app/assets/javascripts/<controller_name>.js

If it doesn't exist, create it. 如果它不存在,请创建它。 If it's a CoffeeScript file, you can just rename it to .js, or write in CoffeeScript, if that's your thing. 如果它是CoffeeScript文件,您只需将其重命名为.js,或者使用CoffeeScript编写,如果这是您的事情。

If you want to keep the code in view file, then put it at the bottom and wrap it in 如果要将代码保存在视图文件中,请将其放在底部并将其包装

<%= content_for :javascript do -%>
   <!-- JS code here -->
<% end -%>

And then place the following right before closing body tag, </body> : 然后在关闭body标签</body>之前放置以下内容:

<%= yield :javascript %>

That way your JS code will get automatically rendered at the bottom, not in the middle of the HTML. 这样,您的JS代码将自动呈现在底部,而不是在HTML的中间。

Maybe the reason is that, after iterating through your loop, are having multiple <img id="choice" /> tags which is invalid HTML. 也许原因是,在遍历循环之后,有多个<img id="choice" />标签是无效的HTML。 Your id is an identifier like you have one as a primary key in your database, it should be unique (at least on that page you're on). 您的id是一个标识符,就像您在数据库中有一个主键一样,它应该是唯一的(至少在您所在的页面上)。

So I would do: 所以我会这样做:

<% Category.select { |category| category.gender == 'girl' }.each do |category| %>
  <img id="choice-<%= category.id -%>" src= <%= category.image %> class="choice new" />
<% end %>

And the modified JS might look like this: 修改后的JS可能如下所示:

$("img.choice").click(function() {
  $(this).toggleClass("new").toggleClass("new1");
});

Remember that this is both untested and straight from my brain. 请记住,这是从我的大脑中未经测试和直接。

edit 编辑
For sake of simplicity, I'd also add a scope to your model, like this: 为简单起见,我还要为您的模型添加一个范围,如下所示:

class Category < ActiveRecord::Base
  scope :female, where('gender = ?', 'female'), :order => :name # or some other ordering
end

So you can do this 所以你可以做到这一点

<% Category.female.each do |category| %>
  [snip]
<% end %>

And your logic is a bit split out into your model, where it should belong IMHO. 你的逻辑有点分裂成你的模型,它应该属于恕我直言。

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

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