【问题标题】:How Can I Check the checkbox that belongs a GridVIew?如何检查属于 GridVIew 的复选框?
【发布时间】:2011-10-03 16:29:44
【问题描述】:

我想做一个 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;
        }
    }
}

【问题讨论】:

    标签: javascript gridview


    【解决方案1】:

    您首先要限制搜索元素的范围。一种方法是使用 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。

    【讨论】:

    • 是的,我在这里问之前看到了,我尝试了这个解决方案,但是var grd,返回null。
    • 确保您输入的是 GridView 的实际渲染 ID,而不是代码中设置的 ID,例如 document.getElementById("");
    • 没有看到你的整个代码,很难说发生了什么,但是上面的应该按照你的意愿工作。正在获取您的网格视图的 ID。
    【解决方案2】:

    要获取特定 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;
            }
        }
    }
    

    【讨论】:

    • 此示例将获取所有复选框?因为我需要点击一个按钮“全选”,然后,所有的都会被选中。
    • @Lucas_Santos:不,当我读到你的问题时,听起来你需要从 gridview 选中一个复选框。我将调整我的答案以匹配网格中的所有复选框。
    • @Lucas_Santos:已发布更新。
    猜你喜欢
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    • 2013-10-16
    • 2012-04-10
    • 2013-03-26
    相关资源
    最近更新 更多