更多javascript运行插件

更多javascript运行插件,javascript,Javascript,我有一个复选框列表。每个人都有一个班,东班或西班。我目前有2个链接,全选,全选。我正在制作两个新链接,选择东,选择西。我使用以下javascript按类选择复选框: <script language="JavaScript"> function checkAll(theForm, cName, status) { for (i=0,n=theForm.elements.length;i<n;i++) if (theForm.e

我有一个复选框列表。每个人都有一个班,东班或西班。我目前有2个链接,全选,全选。我正在制作两个新链接,选择东,选择西。我使用以下javascript按类选择复选框:

<script language="JavaScript">
        function checkAll(theForm, cName, status) {
        for (i=0,n=theForm.elements.length;i<n;i++)
          if (theForm.elements[i].className.indexOf(cName) !=-1) {
            theForm.elements[i].checked = status;
          }
        }

</script>

功能检查全部(表单、cName、状态){
对于(i=0,n=form.elements.length;i也可以替换

onclick="checkAll..."

您的
checkAll
函数必须返回
false


这只是另一种方法。

您缺少东西方链接中的
返回false,因此需要重新加载。此外,我认为您不应该将复选框的选中状态传递到函数中。我认为您的
if
应该类似于

if (theForm.elements[i].className.indexOf(cName) !=-1) {
  // select the ones that are in the class
  theForm.elements[i].checked = true;
}
else {
  // deselect the ones that are not in the class
  theForm.elements[i].checked = false;
}
**更新:**

在这种情况下,您可能希望传入中复选框的状态。您基本上需要更改行为,以便每个链接都成为一个切换。因此,您不需要对其他类执行任何操作,而是反转用户单击的类的状态

if (theForm.elements[i].className.indexOf(cName) !=-1) {
  // toggle the checked status of the current class of checkboxes
  theForm.elements[i].checked = !state;
}

Matti,我又添加了3个2个链接。全选,全选,全选,全选,东选,西选,全选,州选,选择RDA。每个都有自己的类。如果我想同时选择东选和RDA呢?当前使用你的代码如果我选择东,它会选择所有东选并取消选择所有内容。因此,当我单击RDA时,它会删除我所有的东选,等等ly选择类RDA…本质上,如何选择多个选项而不使它们相互覆盖?@Travis:你会想将函数变成一个切换。请参阅上面的更新。嘿,Matti,我已经修改了它,但它再次只是重新加载我的页面。尽管我在单击的最后返回false。你知道吗?试试什么是bluefoot建议。
if (theForm.elements[i].className.indexOf(cName) !=-1) {
  // select the ones that are in the class
  theForm.elements[i].checked = true;
}
else {
  // deselect the ones that are not in the class
  theForm.elements[i].checked = false;
}
if (theForm.elements[i].className.indexOf(cName) !=-1) {
  // toggle the checked status of the current class of checkboxes
  theForm.elements[i].checked = !state;
}