使用 Javascript 在带有键的 PHP 数组中选中/取消选中所有复选框

发布于 2024-12-08 23:59:34 字数 1040 浏览 0 评论 0原文

我有以下代码:

<a href="javascript:setCheckboxes3(1);" class="chkmenu">Check All</a> |
<a href="javascript:setCheckboxes3(0);" class="chkmenu">Uncheck All</a> |
<a href="javascript:setCheckboxes3(2);" class="chkmenu">Invert Selection</a><br />
<table>
<tr>
<td><input type="checkbox" name="names[8]" value="yes" />Paul</td>
<td><input type="checkbox" name="names[11]" value="yes" />Bob</td>
<td><input type="checkbox" name="names[44]" value="yes" />Tom</td>
</tr>
</table>

和以下脚本:

function setCheckboxes3(act)
  {
  elts = document.getElementsByName("names[]");
  var elts_cnt  = (typeof(elts.length) != 'undefined') ? elts.length : 0;
  if (elts_cnt)
    {
    for (var i = 0; i < elts_cnt; i++)
      {
      elts[i].checked = (act == 1 || act == 0) ? act : (elts[i].checked ? 0 : 1);
      }
    }
  }

该脚本正在处理没有键的其他数组,但我无法让它处理这个有键的数组。

提前致谢

I have the following code:

<a href="javascript:setCheckboxes3(1);" class="chkmenu">Check All</a> |
<a href="javascript:setCheckboxes3(0);" class="chkmenu">Uncheck All</a> |
<a href="javascript:setCheckboxes3(2);" class="chkmenu">Invert Selection</a><br />
<table>
<tr>
<td><input type="checkbox" name="names[8]" value="yes" />Paul</td>
<td><input type="checkbox" name="names[11]" value="yes" />Bob</td>
<td><input type="checkbox" name="names[44]" value="yes" />Tom</td>
</tr>
</table>

And the following script:

function setCheckboxes3(act)
  {
  elts = document.getElementsByName("names[]");
  var elts_cnt  = (typeof(elts.length) != 'undefined') ? elts.length : 0;
  if (elts_cnt)
    {
    for (var i = 0; i < elts_cnt; i++)
      {
      elts[i].checked = (act == 1 || act == 0) ? act : (elts[i].checked ? 0 : 1);
      }
    }
  }

The script is working with other arrays without keys, but I can't get it to work with this array which has keys.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阪姬 2024-12-15 23:59:35

您可以使用 getElementsByClassName

<script type="text/javascript" language="javascript">  
function setCheckboxes3(act) {
  var e = document.getElementsByClassName('names');
  var elts_cnt  = (typeof(e.length) != 'undefined') ? e.length : 0;
  if (!elts_cnt) {
    return;
  }
  for (var i = 0; i < elts_cnt; i++) {
    e[i].checked = (act == 1 || act == 0) ? act : (e[i].checked ? 0 : 1);
  }
}
</script> 

<a href="javascript:setCheckboxes3(1);" class="chkmenu">Check All</a> |
<a href="javascript:setCheckboxes3(0);" class="chkmenu">Uncheck All</a> |
<a href="javascript:setCheckboxes3(2);" class="chkmenu">Invert Selection</a><br />
<input type="checkbox" name="names[8]" class="names" value="yes" />Paul
<input type="checkbox" name="names[11]" class="names" value="yes" />Bob
<input type="checkbox" name="names[44]" class="names" value="yes" />Tom

或者您可以使用: getElementsByTagName

<script type="text/javascript" language="javascript"> 
function setCheckboxes3(act) {
  var e = document.getElementsByTagName('input');
  var elts_cnt  = (typeof(e.length) != 'undefined') ? e.length : 0;
  if (!elts_cnt) {
    return;
  }
  for (var i = 0; i < elts_cnt; i++) {
    if((e[i].type) == 'checkbox') {
      e[i].checked = (act == 1 || act == 0) ? act : (e[i].checked ? 0 : 1);
    }
  }
}
</script> 

<a href="javascript:setCheckboxes3(1);" class="chkmenu">Check All</a> |
<a href="javascript:setCheckboxes3(0);" class="chkmenu">Uncheck All</a> |
<a href="javascript:setCheckboxes3(2);" class="chkmenu">Invert Selection</a><br />
<input type="checkbox" name="names[8]" value="yes" />Paul
<input type="checkbox" name="names[11]" value="yes" />Bob
<input type="checkbox" name="names[44]" value="yes" />Tom

您有机会尝试 jQuery 吗?

You can use getElementsByClassName:

<script type="text/javascript" language="javascript">  
function setCheckboxes3(act) {
  var e = document.getElementsByClassName('names');
  var elts_cnt  = (typeof(e.length) != 'undefined') ? e.length : 0;
  if (!elts_cnt) {
    return;
  }
  for (var i = 0; i < elts_cnt; i++) {
    e[i].checked = (act == 1 || act == 0) ? act : (e[i].checked ? 0 : 1);
  }
}
</script> 

<a href="javascript:setCheckboxes3(1);" class="chkmenu">Check All</a> |
<a href="javascript:setCheckboxes3(0);" class="chkmenu">Uncheck All</a> |
<a href="javascript:setCheckboxes3(2);" class="chkmenu">Invert Selection</a><br />
<input type="checkbox" name="names[8]" class="names" value="yes" />Paul
<input type="checkbox" name="names[11]" class="names" value="yes" />Bob
<input type="checkbox" name="names[44]" class="names" value="yes" />Tom

OR you can use: getElementsByTagName

<script type="text/javascript" language="javascript"> 
function setCheckboxes3(act) {
  var e = document.getElementsByTagName('input');
  var elts_cnt  = (typeof(e.length) != 'undefined') ? e.length : 0;
  if (!elts_cnt) {
    return;
  }
  for (var i = 0; i < elts_cnt; i++) {
    if((e[i].type) == 'checkbox') {
      e[i].checked = (act == 1 || act == 0) ? act : (e[i].checked ? 0 : 1);
    }
  }
}
</script> 

<a href="javascript:setCheckboxes3(1);" class="chkmenu">Check All</a> |
<a href="javascript:setCheckboxes3(0);" class="chkmenu">Uncheck All</a> |
<a href="javascript:setCheckboxes3(2);" class="chkmenu">Invert Selection</a><br />
<input type="checkbox" name="names[8]" value="yes" />Paul
<input type="checkbox" name="names[11]" value="yes" />Bob
<input type="checkbox" name="names[44]" value="yes" />Tom

Did you get a chance to try jQuery?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文