【发布时间】:2019-12-27 16:08:52
【问题描述】:
请运行代码 sn-p 查看示例。
So I'm using some radio buttons and I have the third radio button have hidden content unless it's been checked but I can't get it to hide the content again when another radio button is selected.
截图:
选择除最后一个以外的单选按钮时,没有任何显示(效果很好)
选择最后一个单选按钮时,我有它显示下面的字段。 (效果很好)
当我返回选择另一个单选按钮时,项目仍然从第三个单选按钮显示,我希望它们隐藏 - 我的 JS 可能做错了什么?
这里是代码:
document.getElementById('rtd3').addEventListener('change', (e) => {
if (e.target.checked) {
document.getElementById('form-wrapper-certain').style.display = ''
} else {
document.getElementById('form-wrapper-certain').style.display = 'none'
}
});
<div class="custom-control custom-radio mt-3">
<input type="radio" name="rtd[checked]" class="custom-control-input" id="rtd1" value="1" required>
<label class="custom-control-label" for="rtd1"><i>None</i> of the personal information we have collected from you.</label>
</div>
<div class="custom-control custom-radio mt-3">
<input type="radio" name="rtd[checked]" class="custom-control-input" id="rtd2" value="2" required>
<label class="custom-control-label" for="rtd2"><i>All</i> of the personal information we have collected from you (subject to permitted exceptions).</label>
</div>
<div class="custom-control custom-radio mt-3">
<input type="radio" name="rtd[checked]" class="custom-control-input" id="rtd3" value="3" required>
<label class="custom-control-label" for="rtd3"><i>Certain</i> (but not all) personal information we have collected from you.</label>
<div class="invalid-feedback">
Select what information you would like deleted.
</div>
</div>
<div id='form-wrapper-certain' style="display:none">
<div class="row mt-3">
<div class="col-sm-1"></div>
<div class="col-sm-11">
<i>You must specify the personal information you would like us to delete:</i>
</div>
</div>
<div class="row mt-3">
<div class="col-sm-1"></div>
<div class="col-sm-11">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="rtd3Transaction" name="rtd[3][transaction]">
<label class="custom-control-label" for="rtd3Transaction">My transaction data</label>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-sm-1"></div>
<div class="col-sm-11">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="rtd3Device" name="rtd[3][device]">
<label class="custom-control-label" for="rtd3Device">Information about my device(s) collected through cookies and other automated collection tools</label>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-sm-2"></div>
<div class="col-sm-10">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="rtdConfirm" name="rtd[confirm]">
<label class="custom-control-label" for="rtdConfirm">I confirm that I would like not to sell your personal information to third parties.</label>
</div>
</div>
</div>
</div>
【问题讨论】:
-
选择不同的单选选项不会触发
rtd3的change事件。 -
看this thread你会找到很好的信息,this idea可以是你想要的
标签: javascript jquery html