0

有人可以帮我解决这个问题吗

Javascript代码:

function CheckAll(pObj) { 
      var mCurrStatus; 
      mCurrStatus = pObj.checked; 
      var mObjList; mObjList = document.all["process"]; 
      for (x = 0; x <= 2; x++) { 
          var Obj;
          Obj = mObjList[x];
          Obj.checked = mCurrStatus; 
      } 
}

asp-经典代码:

 response.Write "<td nowrap><input type=checkbox name=process_1 " 
 response.Write "/>box1</td>" 
 response.Write "<td nowrap><input type=checkbox name=process_2 " 
 response.Write "/>box2</td>" 

这是我的全选框:

response.Write "<th align=left><input type=checkbox onclick=CheckAll(this)></th>"

当我单击全选框时如何选择那些框(似乎该功能不起作用)请帮助我而不是给我示例...非常感谢你们的帮助~

更新:

我参考了 Roland 的建议并制作了新功能,但没有任何想法?

功能检查所有(){

var eles = [];
var len = elems.length;
//var inputs = document.getElementsByTagName("input");
var elems = tab.getElementsByTagName("input");
for (var i = 0; i < len; i++) {
    if (elems[i].name.indexOf('process_') == 0) {
        eles.push(elems[i]);
    }
}

//var elems = tab.getElementsByTagName("input");
for (var x = 0; x < len; x++) {
    if (elems[x].type == "checkbox") {
        elems[x].checked = true;
    }


}

}

4

1 回答 1

1

这是您可以选择并选中所有复选框的方式:

首先找到你的元素(这是 vanilla.js):

// will find all the html elements that have a name containing the value passed
var find = function(name) {
    // you can optimize this part a bit (pass the tag name)
    var elements = document.getElementsByTagName("*");
    var results = [];
    for(var i = 0; i < elements.length; i++) {
        var elementName = elements[i].name;
        if(elementName !== undefined && elementName.indexOf(name) != -1) {
            results.push(elements[i]);              
        }
    }
    return results;
};

然后这就是您使用此“实用程序”功能检查所有内容的方式:

var checkAll = function() {
    var checkBoxes = find("process");
    for(var i = 0; i < checkBoxes.length; i++) {
        checkBoxes[i].checked = true;
    }
}

这是一个小提琴:

http://jsfiddle.net/BuacB/1/

作为旁注,这里的这一点不是有效的html(你缺少一些引号):

response.Write "<td nowrap><input type=checkbox name=process_1 " 
response.Write "/>box1</td>" 

我相信它需要是:

response.Write "<td nowrap><input type='checkbox' name='process_1' " 
response.Write "/>box1</td>" 
于 2013-08-20T08:28:42.300 回答