1

我有 60 个名为“AG[]”的复选框,我使用全选功能来完成这项工作,并结合了名为 CheckAll 的按钮上的事件监听器。突然按钮停止工作..全选功能是

function selectAll(a,b) {
var checkies = document.getElementsByName('AG[]');
for (var i = a;i < b;i++) {
    checkies[i].checked = !(checkies[i].checked);
    }
}

这行得通,因为我尝试加载该函数的一次运行。这是在按钮上添加事件侦听器的完整脚本 onload。

function script1() {
var el = document.getElementsByName('CheckAll'); 
el1 = el[0]; 
el2 = el[1];
el3 = el[2];
el4 = el[3];
el5 = el[4];
el6 = el[5];
el7 = el[6];


el1.addEventListener('click', function(){selectAll(0,8)}, false);
el2.addEventListener('click', function(){selectAll(8,16)}, false);
el3.addEventListener('click', function(){selectAll(16,26)}, false);
el4.addEventListener('click', function(){selectAll(26,34)}, false);
el5.addEventListener('click', function(){selectAll(34,44)}, false);
el6.addEventListener('click', function(){selectAll(44,52)}, false);
el7.addEventListener('click', function(){selectAll(52,60)}, false);


}

如果我像

SelectAll(0,8);

它可以工作,但如果我通过 addeventlistener 来做,它不会。代码运行良好,我可以用按钮检查所有内容,但我不明白发生了什么.. 这是 jsfiddle jsfiddle

*好的新问题。* Andreas 发布的代码仍然对我不起作用,这可能意味着它是因为我从不支持 addeventlistener 的 IE7 运行它。那么我如何让我的代码支持 firefox/chrome(Addeventlistener) 和

4

3 回答 3

2

牢记addEventListener 与 onclick,实用且简单的版本如下,因为它适用于所有浏览器 - 链接中提到的缺点是 onlick 仅支持一个事件处理程序,而 attachevent/addEventListener 将触发所有已注册的回调。

演示

function selectAll(a,b) {
  var checkies = document.getElementsByName('AG[]');
  for (var i = a;i < b;i++) {
    checkies[i].checked = !checkies[i].checked;
  }
}
function script1(){
  var el = document.getElementsByName('CheckAll');  // get all elements with that name="" attribute
  el[0].onclick=function() { selectAll(0,8) }
  el[1].onclick=function() { selectAll(8,16)}
  el[2].onclick=function() { selectAll(16,26)}
  el[3].onclick=function() { selectAll(26,34)}
  el[4].onclick=function() { selectAll(34,44)}
  el[5].onclick=function() { selectAll(44,52)}
  el[6].onclick=function() { selectAll(52,60)}
}
于 2012-08-22T20:32:03.437 回答
1

我做了一些改变来工作IE < 9,即

addEvent(el1, 'click', function(){selectAll(0,8)},false);
function addEvent(el, event, callback, bubble){
    if(el.addEventListener) el.addEventListener(event, callback, bubble);
    else if(el.attachEvent) el.attachEvent('on'+event, callback, bubble);
}

完成演示

于 2012-08-22T20:32:00.350 回答
0

你忘了function在 script1() 之前加上这个词,所以你得到了一个错误,因为它不知道script1()你的body onload. 我尝试了这段代码(几乎是复制和粘贴(我一直不喜欢 JS fiddle),它似乎可以工作。

<!DOCTYPE html>
<html>
    <style>
        html, body { height: 100%; }
        #container { margin-top: 29px; }
        header { height:29px; margin: 0 49px; background-color:#999999; }

        #div1 { width: 29px; height: 100%; background-color: yellow; display: inline-block; }
        #div2 { width: 100%; height: 100%; background-color: blue; display: inline-block; }
        #div3 { width: 29px; height: 100%; background-color: red; display: inline-block; float: right;}
        #div4 { height: 100%; background-color: yellow; display: inline-block;  float: right; }
    </style>
<body>
<body onload="script1();">
    Please check the box if you took a course in the respective subject in that semester.
<table width="200" border="1">
  <tr>
    <th scope="col"></th>
    <th scope="col">8th Sem 1</th>
    <th scope="col">8th Sem 2</th>
    <th scope="col">9th Sem 1</th>
    <th scope="col">9th Sem 2</th>
    <th scope="col">10th Sem 1</th>
    <th scope="col">10th Sem 2</th>
    <th scope="col">11th Sem 1</th>
    <th scope="col">11th Sem 2</th>
    <th scope="col">12th Sem 1</th>
    <th scope="col">12th Sem 2</th>
    <th scope="col">Select All Semesters</th>
  </tr>
  <tr>
    <th scope="row">History</th>
    <td></td>
    <td></td>
    <td><input name="AG[]" type="checkbox" value="A9Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="A9Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="A10Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="A10Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="A11Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="A11Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="A12Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="A12Sem2"></td>
    <td><input type="button" name="CheckAll" value="Check All"></td>
  </tr>
  <tr>
    <th scope="row">English</th>
    <td></td>
    <td></td>
    <td><input name="AG[]" type="checkbox" value="B9Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="B9Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="B10Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="B10Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="B11Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="B11Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="B12Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="B12Sem2"></td>
    <td> <input type="button" name="CheckAll" value="Check All"></td>
  </tr>
  <tr>
    <th scope="row">Mathematics</th>
    <td><input name="AG[]" type="checkbox" value="C8Sem1">*</td>
    <td><input name="AG[]" type="checkbox" value="C8Sem2">*</td>
    <td><input name="AG[]" type="checkbox" value="C9Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="C9Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="C10Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="C10Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="C11Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="C11Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="C12Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="C12Sem2"></td>
    <td> <input type="button" name="CheckAll" value="Check All"></td>
  </tr>
  <tr>
    <th scope="row">Science</th>
    <td></td>
    <td></td>
    <td><input name="AG[]" type="checkbox" value="D9Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="D9Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="D10Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="D10Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="D11Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="D11Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="D12Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="D12Sem2"></td>
    <td> <input type="button" name="CheckAll" value="Check All"></td>
  </tr>
  <tr>
    <th scope="row">Foreign Language</th>
    <td><input name="AG[]" type="checkbox" value="E8Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="E8Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="E9Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="E9Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="E10Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="E10Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="E11Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="E11Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="E12Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="E12Sem2"></td>
    <td> <input type="button" name="CheckAll" value="Check All"></td>
  </tr>
  <tr>
    <th scope="row">Visual and Performing Arts</th>
    <td></td>
    <td></td>
    <td><input name="AG[]" type="checkbox" value="F9Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="F9Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="F10Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="F10Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="F11Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="F11Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="F12Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="F12Sem2"></td>
    <td> <input type="button" name="CheckAll" value="Check All"></td>
  </tr>
  <tr>
    <th scope="row">Electives</th>
    <td></td>
    <td></td>
    <td><input name="AG[]" type="checkbox" value="G9Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="G9Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="G10Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="G10Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="G11Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="G11Sem2"></td>
    <td><input name="AG[]" type="checkbox" value="G12Sem1"></td>
    <td><input name="AG[]" type="checkbox" value="G12Sem2"></td>
    <td> <input type="button" name="CheckAll" value="Check All"></td>
  </tr>
</table>
</body>
    <script>
function selectAll(a,b) {
var checkies = document.getElementsByName('AG[]');
for (var i = a;i < b;i++) {
    checkies[i].checked = !(checkies[i].checked);
    }
}
function script1(){
var el = document.getElementsByName('CheckAll');  // get all elements with that name="" attribute
el1 = el[0];  // get the first element (in this case, being the only one)
el2 = el[1];
el3 = el[2];
el4 = el[3];
el5 = el[4];
el6 = el[5];
el7 = el[6];

// now we have an element to call the method on

el1.addEventListener('click', function(){selectAll(0,8)}, false);
el2.addEventListener('click', function(){selectAll(8,16)}, false);
el3.addEventListener('click', function(){selectAll(16,26)}, false);
el4.addEventListener('click', function(){selectAll(26,34)}, false);
el5.addEventListener('click', function(){selectAll(34,44)}, false);
el6.addEventListener('click', function(){selectAll(44,52)}, false);
el7.addEventListener('click', function(){selectAll(52,60)}, false);


}
    </script>
</html>
于 2012-08-22T20:27:41.527 回答