简体   繁体   English

如果选中其中一个复选框(每个复选框上的名称不同),如何显示 div?

[英]How to show div, if one of the checkboxes is checked (with a different name on each checkbox)?

I am trying to show div if one of the two checkboxes is checked.如果选中两个复选框之一,我将尝试显示 div。 I found it in some article but with the same name, I am using a different name for each checkbox to store it into mysql. My current javascript code is我在一些文章中找到它但名称相同,我为每个复选框使用不同的名称将其存储到 mysql。我当前的 javascript 代码是

document.addEventListener('change', function(jj) {
  function jj() {
    if ((document.getElementById('jj1_ikk').checked) || (document.getElementById('jj2_ikk').checked)) {
      document.getElementById('jsa').style.display="block";
    } else {
      document.getElementById('jsa').style.display="none";
    }
  }
})

the input fields are输入字段是

<input type="checkbox" id="jj1_ikk" name="jj1_ikk" /><label for="jj1_ikk">A</label>
<input type="checkbox" id="jj2_ikk" name="jj2_ikk" /><label for="jj2_ikk">B</label>

where jj1_ikk and jj2_ikk are the checkboxes id, and jsa is the div that I want to do show/hide.其中 jj1_ikk 和 jj2_ikk 是复选框 ID,jsa 是我想要显示/隐藏的 div。

I hope my description is clear, thank you.我希望我的描述是清楚的,谢谢。

You did a mistake when adding the handler for the change event defining two nested functions.在为定义两个嵌套函数的更改事件添加处理程序时,您犯了一个错误。 Plus I added the event handler only once the document was loaded.另外,我仅在加载文档后才添加事件处理程序。 You can test the code in this snippet:您可以测试此代码段中的代码:

 //when the document has been loaded, adds the event handlers to the checkboxes document.addEventListener("DOMContentLoaded", () => { document.addEventListener('change', () => addHandlers()); }); /** * Adds handler for the change event on both checkboxes */ function addHandlers(){ let jj1 = document.getElementById('jj1_ikk'); let jj2 = document.getElementById('jj2_ikk'); jj1.addEventListener('change', updateMsgVisibility); jj2.addEventListener('change', updateMsgVisibility); } /** * Show/Hide #jsa based on checkboxes status */ function updateMsgVisibility(){ let jj1 = document.getElementById('jj1_ikk'); let jj2 = document.getElementById('jj2_ikk'); if ( (jj1 && (jj1.checked)) || (jj2 && (jj2.checked)) ) { document.getElementById('jsa').style.display="block"; } else { document.getElementById('jsa').style.display="none"; } }
 <input type="checkbox" id="jj1_ikk" name="jj1_ikk" /><label for="jj1_ikk">A</label> <input type="checkbox" id="jj2_ikk" name="jj2_ikk" /><label for="jj2_ikk">B</label> <div id="jsa" style="display:none;">This is the element that will be shown if both checkboxes aren't checked</div>

You can put two check box in span and check changes onclick span like this您可以在跨度中放置两个复选框并检查更改 onclick 跨度这样

HTML HTML

<span onclick="CheckChanges()">
    <input type="checkbox" id="jj1_ikk" name="jj1_ikk" /><label for="jj1_ikk">A</label>
    <input type="checkbox" id="jj2_ikk" name="jj2_ikk" /><label for="jj2_ikk">B</label>
</span>

<div id="jsa">This is the element that will be shown if both checkboxes aren't checked</div>

JavaScript JavaScript

var aCheckBox = document.getElementById("jj1_ikk")
var bCheckBox = document.getElementById("jj2_ikk")

function CheckChanges() {
    if (aCheckBox.checked == true || bCheckBox.checked == true) {
        document.getElementById("jsa").style.display = "block"
    } else {
        document.getElementById("jsa").style.display = "none"
    }
}

暂无
暂无

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

相关问题 当选中一个div中的复选框时,如何取消取消另一个div中的复选框 - How to unckeck checkboxes in another div when checkbox in one div is checked 如何验证在每组复选框中至少选中了一个复选框 - How to validate that at least one checkbox is checked in each set of checkboxes 如果选中了2个特定的复选框,则显示DIV;如果仅选中一个,则显示不同的DIV - Show DIV if 2 specific Checkboxes are checked, if only one checked show different DIV 如果选中了2个特定的复选框,则显示一个div;如果选中了1个复选框,则显示另一个(如果选中了2个复选框,则不显示) - Show a div if 2 specific checkboxes are checked, show another if 1 checkbox is checked (don't show it if the 2 checkboxes are checked) 具体怎么做<div>显示是否选中了具有特定名称的复选框? - How to make specific <div> show if checkbox with specific name is checked off? 如果勾选了一个复选框,如何取消选中所有复选框? - How to uncheck all checkboxes, if one checkbox checked? 如果在一组唯一的复选框中选中一个特定复选框,如何显示特定文本 - How to show specific text if one specific checkbox is checked among bunch of unique checkboxes 当用户首先从某些复选框中选中一个复选框时,如何显示一个 div 框,它可能是 7 个或更多,并在最后取消选中任何复选框时隐藏? - How to show a div box when user firstly checked a checkbox out of some checkboxes it may be 7 or more and hide on last uncheck of any checkbox? 如何检查复选框列表中是否至少选中了一个复选框,其名称属性为数组 - How to check if at least one checkbox is checked from list of checkboxes with it name attribute as array 如果选中复选框,则显示div? - if checkbox is checked show div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM