【问题标题】:Radio button show/hide won't hide on other radio button clicks单选按钮显示/隐藏不会在其他单选按钮点击时隐藏
【发布时间】: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


【解决方案1】:

选择不同的单选选项不会触发rtd3 的更改事件。相反,将事件附加到所有单选按钮,以检查是否应隐藏或显示“某些”部分。

const toggleCertainForm = () => {
  const formWrapper = document.getElementById('form-wrapper-certain');
  const rtd3 = document.getElementById('rtd3');
  formWrapper.style.display = rtd3.checked ? '' : 'none';
}

document.querySelectorAll('[name="rtd[checked]"]').forEach(r => 
  r.addEventListener('change', toggleCertainForm)
);

const toggleCertainForm = () => {
  const formWrapper = document.getElementById('form-wrapper-certain');
  const rtd3 = document.getElementById('rtd3');
  formWrapper.style.display = rtd3.checked ? '' : 'none';
}

document.querySelectorAll('[name="rtd[checked]"]').forEach(r => 
  r.addEventListener('change', toggleCertainForm)
);
<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>

鉴于您已标记 jQuery,这里有一个利用事件委托的替代解决方案:

const toggleCertainForm = () => $('#form-wrapper-certain').toggle($("#rtd3").is(":checked"));
$(document).on("change", '[name="rtd[checked]"]', toggleCertainForm);

const toggleCertainForm = () => $('#form-wrapper-certain').toggle($("#rtd3").is(":checked"));
$(document).on("change", '[name="rtd[checked]"]', toggleCertainForm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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>

【讨论】:

  • 谢谢@Tyler,这里超级干净!
【解决方案2】:

此更改操作:

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

仅当事件 change 在该特定单选按钮(具有 id “rtd3”的单选按钮)中被调用但在其他两个单选按钮中不被调用时触发。

一种可能的解决方案是为其他两个单选按钮重现此行为,例如:

document.getElementById('rtd1').addEventListener('change', (e) => {
    if (e.target.checked) {
        document.getElementById('form-wrapper-certain').style.display = 'none'
    }
});

document.getElementById('rtd2').addEventListener('change', (e) => {
    if (e.target.checked) {
        document.getElementById('form-wrapper-certain').style.display = 'none'
    }
});

或者尝试使用不同的方法,希望对您有所帮助。

【讨论】:

  • 感谢@Luis Miguel,这暂时有效 - 将寻找替代方法来缩小代码范围。
  • @SaintLouisEvents 我很高兴它有帮助,这不是一个很好的解决方案,你绝对应该缩小代码范围,但它确实有效;-)
【解决方案3】:

你好 SaintLouis 活动,

在这种情况下,我认为您缺少复选框和单选按钮的主要属性之一,即checked。

我会做这样的事情:

$("#rtd1").click(function() {
    if($(this).prop("checked")) {
        // this runs when the #rtd1 element is checked
        $("#form-wrapper-certain").hide();
        $("#form-wrapper-certain input[type='checkbox']").prop("checked", false);
        // If you want you can also disable those checkboxes
        $("#form-wrapper-certain input[type='checkbox']").prop("disabled", true);
    }
});

$("#rtd2").click(function() {
    if($(this).prop("checked")) {
        // this runs when the #rtd2 element is checked
        $("#form-wrapper-certain").hide();
        $("#form-wrapper-certain input[type='checkbox']").prop("checked", true);
        // This second line is optional, I assume you are sending all your checkboxes
        // besides from sending the #rtd2 element
        // If you want you can also enable those checkboxes if they have been disabled
        $("#form-wrapper-certain input[type='checkbox']").prop("disabled", false);
    }
});

$("#rtd3").click(function() {
    if($(this).prop("checked")) {
        // this runs when the #rtd3 element is checked
        $("#form-wrapper-certain").show();
        $("#form-wrapper-certain input[type='checkbox']").prop("checked", true);
        // This second line depends on the behavior you want to achieve
        // you can either check all as initial state or uncheck all as initial state
        // If you want you can also enable those checkboxes if they have been disabled
        $("#form-wrapper-certain input[type='checkbox']").prop("disabled", false);
    }
});

if($(this).prop("checked")) 检查可以是可选的,因为单选按钮将始终在单击时检查,除非它们被禁用。

另外,我看到您的示例是用 JavaScript 编写的,但是您在标签中提到了 jQuery,所以我在 jQuery 中做了示例。

更新

将 $(this).is(":checked") 更改为 $(this).prop("checked") 以保留使用 jQuery 的示例并更类似于原始问题。

【讨论】:

  • this.checked 优于 $(this).is(':checked')。伪选择器:checked 主要用于选择器。在这种用法中,您不需要它。您已经拥有 this 和原生 checked 属性,所有浏览器都支持。
  • @Taplar 感谢您的观察。我将is(':checked') 更改为.prop('checked')。
  • 同样,this.checked 在当今时代得到所有浏览器的支持。当属性已经可用时,没有理由获取本机对象、实例化 jQuery 对象并调用方法来获取属性。这种额外的开销是完全没有必要的。 jQuery 很有帮助,可以做很多事情,但你应该时刻警惕陷入“jQuery 必须做所有事情”的心态。
猜你喜欢
  • 2017-11-25
  • 2019-03-19
  • 1970-01-01
  • 2015-03-28
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
  • 2013-01-20
  • 1970-01-01
相关资源
最近更新 更多