简体   繁体   English

如何使用单选按钮隐藏 div

[英]how to hide divs with a radio button

I wish I could show or hide a div following the selection of a radio button我希望我可以在选择单选按钮后显示或隐藏 div
I have 4 radio buttons我有 4 个单选按钮
including 3 which must display the same div包括 3 个必须显示相同 div
and 1 another div还有 1 个 div
the best way i found is this one我找到的最好的方法是这个
But is there a way to avoid having 3 divs with the same content?但是有没有办法避免 3 个 div 内容相同?
I would like to better explain myself that when we select "4 Cars, 3 Cars, 2 Cars" that the id = "liv-1" is simply visible, but that if "because 1" is selected that the id = "liv -1 "or hide我想更好地解释一下自己,当我们 select "4 Cars, 3 Cars, 2 Cars" id = "liv-1" 是简单可见的,但是如果选择 "because 1" 则 id = "liv - 1"或隐藏

 $(document).ready(function() { $("div.desc").hide(); $("input[name$='choix_livraison']").click(function() { var test = $(this).val(); $("div.desc").hide(); $("#ref-" + test).show(); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 2 Cars <input type="radio" name="choix_livraison" class="choix_livraison" data-nom="POSTE-ENVELLOPPE-belgique" value="1" checked="checked"> 3 Cars <input type="radio" name="choix_livraison" class="choix_livraison" data-nom="mondial relay-belgiqueE" value="2"> 3 Cars <input type="radio" name="choix_livraison" class="choix_livraison" data-nom="mondial relay-FRANCE" value="3"> 3 Cars <input type="radio" name="choix_livraison" class="choix_livraison" data-nom="mondial relay-autre" value="4"> <div id="ref-1" class="desc"> j'ai sélectionné 2 </div> <div id="ref-2" class="desc"> j'ai sélectionné 3 </div> <div id="ref-3" class="desc"> j'ai sélectionné 3 </div> <div id="ref-4" class="desc"> j'ai sélectionné 3 </div>

this way:这边走:

  1. use a form使用表格
  2. use labels使用标签
  3. use css使用 css
  4. use the mdn doc -> https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/toggle使用 mdn 文档 -> https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/toggle

 const myForm = document.querySelector('#my-form'), divLiv = document.querySelectorAll('div.desc'), ref_Divs = { '1': 'ref-2', '2': 'ref-3', '3': 'ref-3', '4': 'ref-3' }; myForm.onsubmit = e => e.preventDefault() // disable form page reload; myForm.oninput = e => { let ref = ref_Divs[myForm.choix_livraison.value ] divLiv.forEach(el => { el.classList.toggle('selected', ref===el.id) }) }
 .desc { display: none; }.desc.selected { display: block; } label { display: block; }
 <form id="my-form"> <label> 2 Cars <input type="radio" name="choix_livraison" value="1" class="choix_livraison" data-nom="POSTE-ENVELLOPPE-belgique" checked="checked"> POSTE-ENVELLOPPE-belgique </label> <label> 3 Cars <input type="radio" name="choix_livraison" value="2" class="choix_livraison" data-nom="mondial relay-belgiqueE" > mondial relay-belgiqueE </label> <label> 3 Cars <input type="radio" name="choix_livraison" value="3" class="choix_livraison" data-nom="mondial relay-FRANCE" > mondial relay-FRANCE </label> <label> 3 Cars <input type="radio" name="choix_livraison" value="4" class="choix_livraison" data-nom="mondial relay-autre" > mondial relay-autre </label> </form> <br> <div id="ref-2" class="desc selected"> j'ai sélectionné 2 </div> <div id="ref-3" class="desc"> j'ai sélectionné 3 </div>

Instead of ids use data attribute to use the value you want.而不是 ids 使用 data 属性来使用您想要的值。 Like I assume you want to do it on language so I used data-lang="" .就像我假设你想用语言做的一样,所以我使用了data-lang="" Tweak it to your liking.根据您的喜好调整它。

ps.附言。 you also might want to do a default open one as a value is selected on loading the page.您可能还希望在加载页面时选择默认打开一个值。

 $(document).ready(function() { $("div.desc").hide(); $("input[name$='choix_livraison']").click(function() { var languageCode = $(this).data('lang'); $("div.desc").hide(); $("#ref-" + languageCode).show(); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 2 Cars <input type="radio" name="choix_livraison" class="choix_livraison" data-nom="POSTE-ENVELLOPPE-belgique" data-lang="be" value="1" checked="checked"> 3 Cars <input type="radio" name="choix_livraison" class="choix_livraison" data-nom="mondial relay-belgiqueE" data-lang="be" value="2"> 3 Cars <input type="radio" name="choix_livraison" class="choix_livraison" data-nom="mondial relay-FRANCE" data-lang="fr" value="3"> 3 Cars <input type="radio" name="choix_livraison" class="choix_livraison" data-nom="mondial relay-autre" data-lang="be" value="4"> <div id="ref-be" class="desc"> languageCode BE </div> <div id="ref-fr" class="desc"> languageCode FR </div>

You have only two options.你只有两个选择。 show a) j'ai sélectionné 3 or b) j'ai sélectionné 2. Then you can use a simple if condition in your event handler.显示 a) j'ai sélectionné 3 或 b) j'ai sélectionné 2。然后您可以在事件处理程序中使用简单的 if 条件。 Like that:像那样:

workings example工作示例

 const sel = document.querySelector('#sel'); sel.addEventListener('click', (e) => { const i = document.querySelector('input[name="choix_livraison"]:checked'); let val = e.target.value; console.log('=>',val) if (val == 1) { // show output 1 document.getElementById('output-2').classList.add('hide') document.getElementById('output-1').classList.remove('hide') } else { console.log(2) // show output 2 document.getElementById('output-1').classList.add('hide'); document.getElementById('output-2').classList.remove('hide'); } });
 .hide { display:none; }
 <div id="sel"> 2 Cars <input type="radio" name="choix_livraison" class="choix_livraison" data-nom="POSTE-ENVELLOPPE-belgique" value="1" checked="checked"> 3 Cars <input type="radio" name="choix_livraison" class="choix_livraison" data-nom="mondial relay-belgiqueE" value="2"> 3 Cars <input type="radio" name="choix_livraison" class="choix_livraison" data-nom="mondial relay-FRANCE" value="3"> 3 Cars <input type="radio" name="choix_livraison" class="choix_livraison" data-nom="mondial relay-autre" value="4"> </div> <div id="output-1" class="hide desc"> j'ai sélectionné 2 </div> <div id="output-2" class="hide desc"> j'ai sélectionné 3 </div>

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

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