请运行代码片段以查看示例。
因此,我正在使用一些单选按钮,并且第三个单选按钮具有隐藏内容,除非已经选中,但我无法在选择其他单选按钮时再次隐藏内容。
屏幕截图: 当选择最后一个单选按钮以外的单选按钮时,没有显示任何内容(效果很好)。
当选择最后一个单选按钮时,我让它显示下面的字段。(效果很好)
当我回去选择另一个单选按钮时,第三个单选按钮仍然显示项目,我希望它们被隐藏 - 我的JS可能出了什么问题?
因此,我正在使用一些单选按钮,并且第三个单选按钮具有隐藏内容,除非已经选中,但我无法在选择其他单选按钮时再次隐藏内容。
屏幕截图: 当选择最后一个单选按钮以外的单选按钮时,没有显示任何内容(效果很好)。
![enter image description here](https://istack.dev59.com/TZyBY.webp)
![enter image description here](https://istack.dev59.com/SpzHy.webp)
![enter image description here](https://istack.dev59.com/hnp4t.webp)
这里是代码:
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
事件。 - Tyler Roper