【发布时间】:2012-06-27 13:43:04
【问题描述】:
我对 jQuery 很陌生,所以如果我遗漏了一些明显的东西,我深表歉意,但是……我只想在用户单击复选框时隐藏我的表单的一部分。
所以,我写了这个小 jQuery 脚本:
$("#belonging_entire_world").toggle(function() {
$("#access_for_friends, #access_for_groups").fadeOut();
}, function(){
$("#access_for_friends, #access_for_groups").fadeIn();
});
当我单击标记为“belonging_entire_world”的复选框时,它会隐藏我想要的表单部分(由#access_for_friends 和#access_for_groups 表示的部分),如果我再次单击,它会再次显示该部分但我选中的复选框“belonging_entire_world”始终在视觉上未选中...
如果我在脚本运行后打开 firebug,我会看到我的复选框确实具有 checked=checked 属性...不过,它在浏览器中看起来没有被选中(在所有浏览器中!)...
我尝试在切换功能的第一个功能中添加以下内容:
$("#belonging_entire_world").attr('checked', true);
但仍然......它仍然像未选中......
这让我发疯,我没有看到另一个删除 jQuery 代码的选项,我真的不明白......我把视图的一部分放在下面:
<div id="Belonging-Access-Control">
<h2> Who will be able to see & access this? </h2>
<div class="field" id="entire_world">
<%= f.check_box(:entire_world) %>
<%= f.label(:entire_world, "The entire world!") %>
</div>
<div class="field" id="access_for_friends">
<%= f.check_box(:access_for_friends) %>
<%= f.label(:access_for_friends, "My friends") %>
</div>
和页面的 HTML,在我选中复选框后(萤火虫输出):
<div id="Belonging-Access-Control">
<h2> Who will be able to see & access this? </h2>
<div id="entire_world" class="field">
<input type="hidden" value="0" name="belonging[entire_world]">
<input id="belonging_entire_world" type="checkbox" value="1" name="belonging[entire_world]" checked="checked">
<label for="belonging_entire_world">The entire world!</label>
</div>
<div id="access_for_friends" class="field" style="display: none;">
<input type="hidden" value="0" name="belonging[access_for_friends]">
<input id="belonging_access_for_friends" type="checkbox" value="1" name="belonging[access_for_friends]">
<label for="belonging_access_for_friends">My friends</label>
</div>
【问题讨论】:
-
你用的是什么版本的jQuery?
-
@Andrew: jquery 版本:1.7.2
-
你确定 id 为:beinglonging_entire_world $("#belonging_entire_world").attr('checked', true);参考复选框ID?
-
@Cygnus:我在问题中添加了页面的HTML源代码(仅相关部分)
-
好的,谢谢...只是想清除它 ;-).. 还在寻找
标签: jquery ruby-on-rails checkbox