根据是否选中复选框显示隐藏内容
我对 jquery 还很陌生,所以我为自己到目前为止所掌握的知识感到自豪。我想做的是根据适当的复选框状态显示/隐藏选项列表。一切都工作得很好,但我不知道如何检查加载时是否选中了复选框我知道我现在应该能够使用 is.checked
我有这个
$('.wpbook_hidden').css({
'display': 'none'
});
$(':checkbox').change(function() {
var option = 'wpbook_option_' + $(this).attr('id');
if ($('.' + option).css('display') == 'none') {
$('.' + option).fadeIn();
}
else {
$('.' + option).fadeOut();
}
});
javascript根据复选框的状态淡入和淡出一个类。这是我的 html
<input type="checkbox" id="set_1" checked> click to show text1
<p class = "wpbook_hidden wpbook_option_set_1"> This is option one</p>
<p class = "wpbook_hidden wpbook_option_set_1"> This is another option one</p>
<input type="checkbox" id="set_2"> click to show text1
<p class = "wpbook_hidden wpbook_option_set_2"> This is option two</p>
我遇到的两个问题是 .wpbook_hidden
类的内容始终隐藏,如果在加载时选中该复选框,则应加载内容。
如果有人能弄清楚如何检查加载时盒子的状态,那将非常受欢迎,请随意使用这个jfiddle http://jsfiddle.net/MH8e4/3/
I'm pretty new to jquery so I'm proud of myself for what I've figured out so far. What I'm trying to do is show/hide a list of options depending on the approprate check box status. Eveything is working just fine but I can't figure out how to check to see if a check box is checked on load I know I should be able to use is.checked
right now I have this javascript
$('.wpbook_hidden').css({
'display': 'none'
});
$(':checkbox').change(function() {
var option = 'wpbook_option_' + $(this).attr('id');
if ($('.' + option).css('display') == 'none') {
$('.' + option).fadeIn();
}
else {
$('.' + option).fadeOut();
}
});
Which fades in and out a class depending on the status of the checkbox. This is my html
<input type="checkbox" id="set_1" checked> click to show text1
<p class = "wpbook_hidden wpbook_option_set_1"> This is option one</p>
<p class = "wpbook_hidden wpbook_option_set_1"> This is another option one</p>
<input type="checkbox" id="set_2"> click to show text1
<p class = "wpbook_hidden wpbook_option_set_2"> This is option two</p>
The two problems I have are that the content with the .wpbook_hidden
class is always hidden and if the checkbox is checked on load the content should load.
If someone could figure out how to check the status of the box on load that would be much appriciated feel free to play with this jfiddle http://jsfiddle.net/MH8e4/3/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
:checked
作为选择器。像这样,更新的小提琴
或者如果你想检查特定复选框的状态,这将是 一样
更新的小提琴
就像下面的评论的
,我已经编辑了你的代码,现在看起来像这样。 #更新了小提琴
you can use
:checked
as a selector. like this,updated fiddle
or if you want to check the state of a particular checkbox, it would be something like this
updated fiddle
for the comment below, I have edited your code and now looks like this.
#updated fiddle
要检查复选框是否被选中,您可以使用以下代码:
to checkout if a checkbox is checked you can use the following code :
试试这个:
Try this: