简体   繁体   English

更改事件Javascript的复选框

[英]Checkbox on change event Javascript

In my page I have an array of string and in my script I have a function that excludes the strings that contain certain char. 在我的页面中,我有一个字符串数组,在我的脚本中,我有一个函数将包含某些字符的字符串排除在外。

I control it through 3 check boxes. 我通过3个复选框控制它。

I have the functions to do this but my problem is when I check two check boxes one to exclude the strings containing 'a' and one containing 'e'. 我具有执行此操作的功能,但是我的问题是,当我选中两个复选框以排除包含“ a”的字符串和一个包含“ e”的字符串时。 I get the result of the last checked check box. 我得到最后选中的复选框的结果。

How to make a function that will handle all the check boxes and show me the final result. 如何使函数能够处理所有复选框并显示最终结果。

This is what I have so far : 这是我到目前为止所拥有的:

My html : 我的html:

<div id="demo"> </div>
<div class="item">
   <form id="aForm"  onchange="filter()">
      <input type="checkbox" id ="A" value="A">Exclude words with 'A'<br>
      <input type="checkbox" id ="E" value="E">Exclude words with 'E'<br>
      <input type="checkbox" id ="O" value="O">Exclude words with 'O'<br>
   <form id="aForm" >
</div> 

<script type="text/javascript">
var animals = ["Bear", "Mouse", "Cat", "Tiger", "Lion"]

function filter () {
    if(document.getElementById('A').checked) {
       var result2 = [];
       for (var animal in Animals) {
          if (Animals[animal].indexOf('a') == -1) {
             result2 += " " + Animals[animal];
          }
          document.getElementById("demo").innerHTML = result2;
        }
     }
    if(document.getElementById('E').checked) {
       var result2 = [];
       for (var animal in Animals) {
           if (Animals[animal].indexOf('e') == -1) {
              result2 += " " + Animals[animal];
            }
            document.getElementById("demo").innerHTML = result2;
        }
     }
    if(document.getElementById('O').checked) {
      var result2 = [];
      for (var animal in Animals)   {
         if (Animals[animal].indexOf('o') == -1) {
            result2 += " " + Animals[animal];
         }
        document.getElementById("demo").innerHTML = result2;
      }
    }
}

Because whatever changes was done by the previous checks are overridden by the last if block if it is checked 因为以前的检查所做的任何更改都被最后的if块覆盖(如果已检查)

 var animals = ["Bear", "Mouse", "Cat", "Tiger", "Lion"]; function filter() { var a = document.getElementById('A').checked, e = document.getElementById('E').checked, o = document.getElementById('O').checked, result2; //make a copy result2 = animals.filter(function(value) { value = value.toLowerCase(); return (!a || value.indexOf('a') == -1) && (!e || value.indexOf('e') == -1) && (!o || value.indexOf('o') == -1); }) document.getElementById("demo").innerHTML = result2; } filter(); 
 <div id="demo"> </div> <div class="item"> <form id="aForm" onchange="filter()"> <input type="checkbox" id ="A" value="A"/>Exclude words with 'A'<br/> <input type="checkbox" id ="E" value="E"/>Exclude words with 'E'<br/> <input type="checkbox" id ="O" value="O"/>Exclude words with 'O'<br/> </form> </div> 

Below logic code should work fine 下面的逻辑代码应该可以正常工作

 var Animals = ["Bear", "Mouse", "Cat", "Tiger", "Lion"] function filter () { var result2 = []; document.getElementById("demo").innerHTML = ""; for (var animal in Animals) { //Copy the value to a temp variable which you can manipulate thisAnimal = Animals[animal]; //Check if each check-box is checked and if so, does the value in variable contains the stop-character //If it has, blank out the temp variable if (document.getElementById('A').checked && thisAnimal.indexOf('a') == -1) thisAnimal = ""; if (document.getElementById('E').checked && thisAnimal.indexOf('e') == -1) thisAnimal = ""; if (document.getElementById('O').checked && thisAnimal.indexOf('o') == -1) thisAnimal = ""; //If the temp variable is not blank, due to one of the above conditions, then append it to the final result if(thisAnimal.length > 0) result2 += " " + thisAnimal; document.getElementById("demo").innerHTML = result2; } } // Call the function to display the result for the initial run filter (); 
 <div id="demo"> </div> <div class="item"> <form id="aForm" onchange="filter()"> <input type="checkbox" id ="A" value="A">Exclude words with 'A'<br> <input type="checkbox" id ="E" value="E">Exclude words with 'E'<br> <input type="checkbox" id ="O" value="O">Exclude words with 'O'<br> </form> </div> 

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

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