简体   繁体   English

JavaScript Radio Toggle根据已选中显示不同的表单字段

[英]JavaScript Radio Toggle Show Different Form Fields Based on Checked

So I'm trying to display certain fields based on the radio that's selected and I feel like I'm close but it's not working and I'm not getting errors. 因此,我试图根据所选的电台显示某些字段,感觉好像已经接近,但无法正常工作,并且没有收到错误消息。

So I defined all the DOM elements, set change event listeners, and I'm calling them down. 因此,我定义了所有DOM元素,设置了更改事件侦听器,然后将其调用。 I also set a default of display: none on a class that works. 我还设置了显示的默认值:在有效的类上没有显示。

 const intSelect = document.querySelector('#internal_form'); const extSelect = document.querySelector('#external_url'); const intForm = document.querySelector('.internal_form'); const extForm = document.querySelector('.external_form'); function show() { intSelect.addEventListener('change', showOne); extSelect.addEventListener('change', showOne); } let showOne = event => { event.preventDefault(); showTheThings(event.currentTarget, intForm, extForm); } let showTheThings = (target, div) => { if (target.checked = true) { div.classList.remove('hide'); } else { div.classList.add('hide'); div.querySelectorAll('input[type="radio"]').forEach(function(el) { el.checked = false; }); } }; 
 .external_form.hide, .internal_form.hide{ display: none; } 
 <form> <div class="form-group radio-format"> <label>Method</label> <div class="col-sm-10"> <input class="trigger form-check-input" type="radio" value="Internal Form" name="event[registration]" id="internal_form"> <label class="form-check-label" for="internal_form">Internal Form</label> <input class="trigger form-check-input" type="radio" value="External URL" name="event[registration]" id="external_url"> <label class="form-check-label" for="external_url">Internal Form</label> </div> </div> <div class="internal_form hide"> <h2>Internal</h2> </div> <div class="external_form hide"> <h2>External</h2> </div> </form> 

However nothing is happening. 但是什么也没发生。 Though when I test in my browser it will apply the change on the intSelect but will not remove when clicking on extSelect (weird how it's not working here). 虽然当我在浏览器中进行测试时,它将在intSelect上应用更改,但是在单击extSelect时将不会将其删除(这在这里不起作用)。

I've also tried very generic JS with something like: 我也尝试了非常通用的JS,例如:

function show(){
 if (intSelect.checked =true) {
  intForm.classList.remove('hide');
 } else {
  intForm.classList.add('hide');
 };
}

This again works just on removing the class but not triggering the add. 这仅适用于删除类,但不会触发添加。

What am I missing here? 我在这里想念什么?

So trying out your code locally, I made a couple of changes. 因此,在本地试用您的代码,我进行了一些更改。

First was to make show function as a self-calling function so that the event listener will be attached on the radio buttons as soon as the page is loaded. 首先是将show函数作为自调用函数,以便在页面加载后将事件侦听器附加到单选按钮上。

(function show() {
    intSelect.addEventListener("click", showOne);
    extSelect.addEventListener("click", showOne);
})();

Also, you should make sure that the script is loaded when the html/DOM is loaded by adding the script tag at the end of the body tag of the HTML. 另外,您应该通过在HTML的body标签的末尾添加script标签来确保在加载html / DOM时加载了脚本。

body>
    <form>
        <div class="form-group radio-format">
            <label>Method</label>
            <div class="col-sm-10">
                <input class="trigger form-check-input" type="radio" value="Internal Form" name="event[registration]"
                    id="internal_form">
                <label class="form-check-label" for="internal_form">Internal Form</label>
                <input class="trigger form-check-input" type="radio" value="External URL" name="event[registration]"
                    id="external_url">
                <label class="form-check-label" for="external_url">External Form</label>
            </div>
        </div>
        <div class="internal_form hide">
            <h2>Internal</h2>
        </div>
        <div class="external_form hide">
            <h2>External</h2>
        </div>
    </form>
    <script src="index.js"></script>

</body>

So, now your js looks like this: 因此,现在您的js如下所示:

const intSelect = document.querySelector("#internal_form");
const extSelect = document.querySelector("#external_url");
const intForm = document.querySelector(".internal_form");
const extForm = document.querySelector(".external_form");

(function show() {
    intSelect.addEventListener("click", showOne);
    extSelect.addEventListener("click", showOne);
})();

function showOne(event) {
    event.preventDefault();
    showTheThings(event.currentTarget, intForm, extForm);
}

function showTheThings(target, div) {
    if ((target.checked = true)) {
        div.classList.remove("hide");
    } else {
        div.classList.add("hide");
        div.querySelectorAll('input[type="radio"]').forEach(function(el) {
            el.checked = false;
        });
    }
}

I hope this helps. 我希望这有帮助。

Here is the simple code using the ID , And by add and removing class 这是使用ID的简单代码,并通过add and removing class

<!DOCTYPE html>
<head>
    <!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> -->
    <style type="text/css">
        .external_form.hide,
.internal_form.hide{
display: none;
}
    </style>
</head>
<body >
<form>
  <div class="form-group radio-format">
    <label>Method</label>
    <div class="col-sm-10">
      <input class="trigger form-check-input" type="radio" value="Internal Form" name="event[registration]" id="internal_url">
      <label class="form-check-label" for="internal_url">Internal Form</label>
      <input class="trigger form-check-input" type="radio" value="External URL" name="event[registration]" id="external_url">
      <label class="form-check-label" for="external_url">external Form</label>
    </div>
  </div>
  <div id="internal" class="internal_form hide">
    <h2>Internal</h2>
  </div>
  <div id="external" class="external_form hide">
   <h2>External</h2>
  </div>
</form>
</body>
</html>
<script>
    var internal = document.getElementById("internal");
    var external = document.getElementById("external");

document.getElementById("internal_url").addEventListener("click", function(){
    internal.classList.remove("hide");
    external.classList.add("hide");
    console.log("internal" , internal);
});

document.getElementById("external_url").addEventListener("click", function(){
    internal.classList.add("hide");
    external.classList.remove("hide");
});


</script>

Code is simple and by looking it , you will get understand . 代码很简单,通过查看它,您将了解。 If you have any doubt , feel free to ask anytime . 如果您有任何疑问,请随时询问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM