jQuery在Rails 3.2应用程序中不起作用? [英] Jquery not working in rails 3.2 app?

查看:99
本文介绍了jQuery在Rails 3.2应用程序中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图进行简单的显示/隐藏..在js中工作只是在我的应用中没有效果.

Trying to get a simple show/hide going.. Its working in js fiddle just not in my app..

在我的观点/user_steps/interests.html.erb

In my views/user_steps/interests.html.erb

<label class="checkbox">
 <input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="2" />
 Valentine Day
</label>
<div style="display:none;">
    Whatever you have to capture here<input type="text" id="foo" />
</div>
</br>
<label class="checkbox">
 <input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="3" />
 Easter
</label>
<div style="display:none;">
    Whatever you have to capture here<input type="text" id="foo1" />
</div>
</br>
<label class="checkbox">
 <input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="4" />
 Mother Day
</label>
<div style="display:none;">
    Whatever you have to capture here<input type="text" id="foo2" />
</div>
</br>
<label class="checkbox">
  <input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="5" />
  Father's Day
</label>
<div style="display:none;">
    Whatever you have to capture here<input type="text" id="foo3" />
</div>
</br>
<label class="checkbox">
 <input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="6" />
 Halloween
</label>
<div style="display:none;">
    Whatever you have to capture here<input type="text" id="foo4" />
</div>
</br>
<label class="checkbox">
 <input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="7" />
 Thanksgiving
</label>
</br>
<label class="checkbox">
  <input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="8" />
  Christmas
</label>

在我的资产/javascripts/user_steps.js中:

In my assets/javascripts/user_steps.js:

$(".checkbox").click(function(){
   $(this).next("div").toggle();  
});

推荐答案

不要重复DOM元素的ID. ID在页面上应该是唯一的,如果不是,则javascript选择器可能会变得不可靠.

Don't duplicate IDs for DOM elements. ID should be unique on the page, and if it's not, javascript selectors can become unreliable.

这篇关于jQuery在Rails 3.2应用程序中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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