如果选中“其他”无线电或复选框,则显示/隐藏div

我意识到这与已经提出的许多其他问题并没有太大的不同,但正是这些差异让我变得棘手。

基本上,如果选中“other”,我想显示或隐藏文本输入以指定“其他”选项。

我的方法似乎适用于复选框。 但是当您取消选中另一个单选按钮时,其他文本输入将不会隐藏。 我知道无线电的性质有点不同,但我不知道如何适应这一点。

这是代码,我还创建了一个jsFiddle ,你可以在其中看到它的实际效果。 谢谢你的帮助!

HTML

Pick One

Pick Several

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;} 

这是我的方法:在整个无线电“小组”工作:

 $('input[name=options]').click(function(){ if ($(this).hasClass('other')) { $(this).parents('fieldset').children(".specify-other").show('fast'); } else { $(this).parents('fieldset').children(".specify-other").hide('fast'); } }) 

确定无论何时单击单选按钮,它总是检查,它不像复选框一样检查或取消选中单击。

您检查了具有某些条件的单选按钮作为所有单选按钮的组,因此请检查值,如果选中value = 4,则显示其他愿望隐藏