已经搜索了谷歌和堆积溢出并找到了一些很好的建议,但是无论我尝试什么都不能正常工作。基本上,如果无线电组设置为“是”,我会尝试显示和隐藏 div。
我曾尝试使用类来实现这一点,但我根本无法让它工作,尽管我相信我误解了这个概念。
值得注意的是,我们也需要它的多个实例才能工作:)
To sum up, i need 2 radio buttons for yes and no, when yes is selected, the next direct div with a class of extra-field needs to display and then hide again if the radio button is selected to no. 我相信需要使用 $(this) 来查找下一个 .extra-field 以仅显示
为了方便,我创建了一个 jsFiddle - http://jsfiddle.net/andyjh07/GmYjd/并粘贴了以下代码:
HTML -
<label class="label2">Do you have a 2nd Occupation?</label>
<input class="checkme" type="radio" name="22. Do you have 2nd Occupation" value="Yes" />Yes
<input type="radio" name="22. Do you have 2nd Occupation" value="No" />No<br />
<div class="extra-field">
<label class="label">Secondary Occupation?</label> <input class="textbox requiredField" name="22a. If Yes - Secondary Occupation?" type="text" />
<div class="clear"> </div>
</div>
和 jquery -
// Trigger extra fields to show depending on what checkme class is clicked
$(".checkme").on('click', function(){
$(this).next(".extra-field").fadeToggle(500);
});