简体   繁体   English

复选框上的Javascript事件监听器

[英]Javascript event listener on a checkbox

I'm trying to hide/show checkboxes based on a button click('send email' or 'cancel'). 我试图基于按钮单击(“发送电子邮件”或“取消”)隐藏/显示复选框。 The button hide show works, but I also added an event listener to the checkboxes to change the button text to 'confirm' if any check box was active. 按钮隐藏显示有效,但是我还向复选框添加了事件侦听器,以将按钮文本更改为“确认”(如果有任何复选框处于活动状态)。 It seems there's a delay in when a checkbox registers as 'checked' and I can't think of or find a better way to check it. 复选框注册为“已选中”时似乎存在延迟,我想不出或找不到更好的方法进行检查。 Any help would be greatly appreciated! 任何帮助将不胜感激!

 var checker = document.getElementById("emailer"); var radios = document.querySelectorAll(".radioEmail+label"); for (var i=0; i<radios.length; i++){ radios[i].addEventListener('click', function(){ var checkingChecks = document.querySelectorAll(".radioEmail:checked").length; if (checkingChecks > 0) { document.getElementById("emailer").className="confirm"; } else { document.getElementById("emailer").className="cancel" } } )}; document.getElementById("emailer").addEventListener('click', function(){ if (checker.getAttribute('class')=='emailer'){ checker.className = "cancel"; for(var i = 0;radios.length>i;i++) { radios[i].style.display="inline-block" } } else if (checker.getAttribute('class')=='cancel') { for(var i = 0;radios.length>i;i++) { radios[i].style.display="none" } checker.className="emailer"; } }); 
  input[type=checkbox].radioEmail + label{display:none} .emailer:after{content:"Send Mass Email";} .confirm:after{content:"confirm"} .cancel:after{content:"cancel"} input[type=checkbox] + label{ cursor:pointer; width : 1em; font-size : 0.875em; line-height : 1em; color : white; text-align : center; font-weight : bold; height : 1em; margin : 0.25em 0.5em 0.25em 0.25em; border : 0.0625em solid #000066; border-radius : 0.25em; background : white; vertical-align : bottom; transition: ease-in-out 0.3s; -webkit-transition: ease-in-out 0.3s;} input[type=checkbox].radioEmail + label{display:none} input[type=checkbox]:checked + label{background:#000066} input[type=checkbox]:checked + label:before{content: '✓'} input:hover + label{background:rgba(0,0,102,0.5)} input[type=checkbox]{ width : 2em; margin : 0; padding : 0; font-size : 1em; opacity : 0;} 
 <button id = "emailer" class='emailer' ></button> <input class="radioEmail" id="option" type="checkbox" name="field" value="option"> <label for="option"></label> <input class="radioEmail" id="option2" type="checkbox" name="field" value="option"> <label for="option2"></label> 

jsfiddle example jsfiddle示例

Your radios variable contains a collection of the radio labels: 你的radios变量包含无线标签的集合:

var radios = document.querySelectorAll(".radioEmail+label");

When a label is clicked, the click event is triggered before the input value actually changes. 单击标签后,将实际更改输入值之前触发click事件。

I would normally recommend putting inputs within their labels. 我通常建议将输入内容放在其标签内。 However,you have a lot of input + label CSS that would break, and I don't know what other code you might have that could also break. 但是,您有很多input + label CSS可能会损坏,而且我不知道您可能还有哪些其他代码也可能会损坏。

A quick solution given your current HTML and CSS would be to check the radio buttons within a setTimeout() : 给定当前HTML和CSS的一种快速解决方案是检查setTimeout()中的单选按钮:

radios[i].addEventListener('click',
  function() {
    setTimeout(function() {
      var checkingChecks = document.querySelectorAll(".radioEmail:checked").length;
      if (checkingChecks > 0) {
        checker.className = "confirm";
      } else {
        checker.className = "cancel";
      }
    });
  }
)

Working Fiddle 工作小提琴

<input type="checkbox" id="the_box" />

 $(document).ready(function(){ 
    %('#the_box').on('change', function(){ 
       if ($(this).is(':checked')){
            //checkbox is checked, do something
           }

   })


})

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

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