如何勾选属于GridVIew的复选框?

How Can I Check the checkbox that belongs a GridVIew?

本文关键字:复选框 GridVIew 何勾选 属于      更新时间:2023-09-26

我想做一个javascript函数,检查未选中的复选框。我现在的功能是选中所有未选中的复选框,我需要选中特定的GridView未选中的复选框

function checar() {    
    var el = document.getElementsByTagName("input");
    for (var i = 0; i < el.length; i++) {
        if (el[i].type == "checkbox") {
            el[i].checked = true;
        }
    }
}

首先要限制元素的搜索范围。一种方法是使用getElementById

function checar() { 
var grd = document.getElementById("<%=grd.ClientID%>"); // <-- Add this line
var el = grd.getElementsByTagName("input"); // <-- change the scope of this to grd
//rest of your code here.
}

示例使用div,但你会得到的想法,我认为:http://jsfiddle.net/8LRkk/

编辑包括设置特定的网格ID。

要获取特定gridview的所有复选框,您需要抓取其ClientID包含gridview的ClientID部分的复选框,因为所有控件都有一个id为"堆叠"。

你的函数应该作为一个基础,它只需要有一个额外的检查:

function checar() {    
    var el = document.getElementsByTagName("input");
    // Get the client id of the gridview from ASP here
    var gvID = '<%= this.myGridview.ClientID %>';
    for (var i = 0; i < el.length; i++) {
        // Call the id of the checkbox and check to see if it
        // contains the client id of the gridview at the same time
        if (el[i].type == "checkbox" && el.id.indexOf(gvID) != -1) {
            el[i].checked = true;
        }
    }
}