【发布时间】:2011-09-01 19:34:32
【问题描述】:
我意识到这与许多其他问题并没有太大的不同,但正是这些细微的差异让我觉得很棘手。
基本上,如果选中“其他”,我想显示或隐藏文本输入以指定“其他”选项。
我的方法似乎适用于复选框。但是当您取消选中其他单选按钮时,其他文本输入不会隐藏。我知道收音机的性质有点不同,但我不确定如何适应这一点。
这是代码,我还创建了一个jsFiddle,您可以在其中看到它的运行情况。感谢您的帮助!
HTML
<fieldset>
<h1>Pick One</h1>
<p><label><input value="1" type="radio" name="options"> Chicken</label></p>
<p><label><input value="2" type="radio" name="options"> Beef</label></p>
<p><label><input value="3" type="radio" name="options"> Fish</label></p>
<p><label><input value="4" type="radio" name="options" class="other"> Other</label></p>
<p class="specify-other"><label>Please specify: <input name="options-other"></label></p>
</fieldset>
<fieldset>
<h1>Pick Several</h1>
<p><label><input value="1" type="checkbox" name="food"> Rice</label></p>
<p><label><input value="2" type="checkbox" name="food"> Beans</label></p>
<p><label><input value="3" type="checkbox" name="food"> Potatoes</label></p>
<p><label><input value="4" type="checkbox" name="food" class="other"> Other</label></p>
<p class="specify-other"><label>Please specify: <input name="options-other"></label></p>
</fieldset>
jQuery
>$('.other').click(function(){
if ($(this).is(':checked'))
{
$(this).parents('fieldset').children(".specify-other").show('fast');
} else {
$(this).parents('fieldset').children(".specify-other").hide('fast');
}
});
CSS
.specify-other {display: none;}
【问题讨论】:
-
您可以将
$(this).is(':checked')更改为this.checked- 它更加更高效,并且语义相同。
标签: jquery