关于更改事件Javascript的复选框

时间:2015-05-12 12:01:59

标签: javascript checkbox onchange

在我的页面中,我有一个字符串数组,在我的脚本中,我有一个函数可以排除包含某些字符串的字符串。

我通过3个复选框控制它。

我有这样做的功能,但我的问题是当我检查两个复选框时,排除包含'a'的字符串和包含'e'的字符串。我得到最后一个选中复选框的结果。

如何创建一个能够处理所有复选框并向我显示最终结果的函数。

这是我到目前为止所做的:

我的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;
      }
    }
}

2 个答案:

答案 0 :(得分:1)

因为先前的检查所做的任何更改都被最后一个if块覆盖(如果已选中)

&#13;
&#13;
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();
&#13;
<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> 
&#13;
&#13;
&#13;

答案 1 :(得分:1)

下面的逻辑代码应该可以正常工作

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>