admin管理员组文章数量:1181872
Basically, I want to only show these fields if checkbox is selected, if it becomes unselected, disappear.
<input type="checkbox" name="supplied" value="supplied" class="aboveage2" />
<ul id="date">
<li><input id="start" name="start" size="5" type="text" class="small" value="1" /></li>
<li><input id="end" name="end" size="5" type="text" class="small" value="2" /></li>
</ul>
I've tried something like:
$('#supplied').live('change', function(){
if ( $(this).val() === 'supplied' ) {
$('.date').show();
} else {
$('.date').hide();
}
});
Any advice would be greatly appreciated =)
Basically, I want to only show these fields if checkbox is selected, if it becomes unselected, disappear.
<input type="checkbox" name="supplied" value="supplied" class="aboveage2" />
<ul id="date">
<li><input id="start" name="start" size="5" type="text" class="small" value="1" /></li>
<li><input id="end" name="end" size="5" type="text" class="small" value="2" /></li>
</ul>
I've tried something like:
$('#supplied').live('change', function(){
if ( $(this).val() === 'supplied' ) {
$('.date').show();
} else {
$('.date').hide();
}
});
Any advice would be greatly appreciated =)
Share Improve this question asked Feb 28, 2011 at 1:10 LatoxLatox 4,69515 gold badges50 silver badges74 bronze badges6 Answers
Reset to default 17The "#foo" selector looks for elements whose id value is "foo", not "name". Thus the first thing you need to do is add an "id" attribute to your checkbox.
The second thing to worry about is the fact that, in IE (at least old versions), the "change" event isn't fired until the checkbox element loses focus. It's better to handle "click", and what you want to check is the "checked" attribute of the element.
What I'd write is something like:
$('#supplied').click(function() {
$('.date')[this.checked ? "show" : "hide"]();
});
Pointy pointed out that you need to set the id of our checkbox (or use a name selector). You also need to use #date
(id) instead of .date
(class) (or again change the HTML).
Working demo
You can do this with pure CSS3, of course:
:checked + #date { display: block; }
#date { display: none; }
The equivalent selectors ought to work just fine in jQuery as well.
Try this:
$('input[name=supplied]').live('change', function(){
if ( $(this).is(":checked")) {
$('#date').show();
} else {
$('#date').hide();
}
});
Matthews answer works great just that the .live deprecated in jQuery 1.7 use the .on
$('#supplied').on('change', function(){
if ( $(this).is(':checked') ) {
$('#date').show();
} else {
$('#date').hide();
}
});
Try something like:
$('#supplied').live('change', function(){
if ( $(this).attr("checked")) {
$('.date').show();
} else {
$('.date').hide();
}
});
本文标签: javascriptHow to only show input fields if checkbox is checkedStack Overflow
版权声明:本文标题:javascript - How to only show input fields if checkbox is checked? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738270794a2072254.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论