我想实现 选中Gridview中的Checkbox后,利用js 获取被选中这一行的其他字段的数据。

解决方案 »

  1.   

    if(checked)
    {
      this.parentElement.children.item(index);
    }
      

  2.   

    checkbox上面应该绑定了一个关于该列信息的动态值吧,根据这个值取得其它的数据:)
      

  3.   

    随便想个方法checkbox的ID设置为  a<%# DataBinder.Eval(Container.DataItem,"id")%>后面的其他字段依次<asp:Lable ID=b<%# DataBinder.Eval(Container.DataItem,"id")%>
    runat=server Text=<%# DataBinder.Eval(Container.DataItem,"name")%> />
    ...选择checkbox的时候将自己传过去onclick=ustbwuyi(this)function ustbwuyi(obj)
    {
     var id=obj.id;
      id=id.substring(id.indexOf("a")+1,id.length);
    var b=document.getElementById("b"+id).value;
    }
      

  4.   

    写得罗嗦麻烦,SassyBoy(The Making Of Slave~~~) 的方法如果可行则为佳
      

  5.   

    var oTbody=document.all.gridView.children.item(0);
            for(var i=1;i<oTbody.children.length;i++)
            {
                 var oTr=oTbody.children.item(i);
                 
                 var 字段1=oTr.children.item(0).innerText
                 var 字段2=oTr.children.item(1).innerText;
            }
      

  6.   

    上面是取所有的,如果取选中的,只需判断checkbox.checked=true并取得它的上级元素tr即可。
      

  7.   

    给你个例子参考一下var rowCount = tb.rows.length;
    var cellCount = tb.rows[1].cells.length;

    for(var m=1;m<rowCount;m++)
    {
    for(var n=1;n<cellCount;n++)
    {
    var cell = tb.rows[m].cells[n];
    if(typeof cell!="undefined" && cell!=null)
    {
    if(cell.style.backgroundColor=="blue")
    cell.style.backgroundColor="white";
    }
    }
    }
      

  8.   

    var RegString = new RegExp("(.*?)" + ctl00_ContentPlaceHolder1_GridView1); 
                var sn = false;
                var A2 = null;
                for(i = 0; i < document.all.length; i = i + 1) 
                { 
                    if (RegString.test(document.all[i].id)) 
                    { 
                        //获取查找到的gridView对象 
                        var grid = document.getElementById(document.all[i].id);
                        //var oTbody = grid.children.item(0);
                         
                        //循环执行选中操作 
                        for(i = 0; i < grid.all.length; i = i + 1) 
                        { 
                            var RegString = new RegExp("(.*?)" + ctl00_ContentPlaceHolder1_GridView1); 
                            if (RegString.test(grid.all[i].id)) 
                            { 
                                if (grid.all[i].checked)
                                {
                                    sn = true;//说明有选中的
                                    
                                }                            
                            } 
                        } 
                        break; 
                    } 
                }
                
                if (sn)
                { 
                    //说明有选中的,可以进行处理....
                }
      

  9.   

    给个参考..//aspx.cs
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkSelect = (CheckBox)e.Row.FindControl("chkSelect");
                chkSelect.Attributes.Add("onclick", "show ('" + e.Row.RowIndex + "')");
            }
        }
    //aspx页添加脚本
        function show(index)
    {
        var table = document.getElementById("GridView1");
        for(var i = 1; i < table.rows[0].cells.length; i++)
                alert (table.rows[index + 1].cells[i].innerText);
        return false;
    }
      

  10.   

    完整示例代码:功能
    1, 单击 checkbox 返回当前行值
    2, 外部按钮获取所有选择行的值
    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Data" %><%-- http://community.csdn.net/Expert/TopicView3.asp?id=5653656 --%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                LoadProductData();
            }
        }    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            Button btnHiddenPostButton = e.Row.FindControl("btnHiddenPostButton") as Button;
            if (btnHiddenPostButton != null) {
                e.Row.Attributes["onclick"] = String.Format("javascript:document.getElementById('{0}').click()", btnHiddenPostButton.ClientID);
                // 额外样式定义
                e.Row.Attributes["onmouseover"] = "javascript:this.style.background='red'";
                e.Row.Attributes["onmouseout"] = "javascript:this.style.background=''";
                e.Row.Attributes["style"] = "cursor:pointer";
                e.Row.Attributes["title"] = "单击选择当前行";
            }
            // 若希望将隐藏按钮单独放于一列,则设置此列隐藏,占位符 <cellIndex> 表示此列索引
            //e.Row.Cells[<cellIndex>].Attributes["style"] = "display:none";
        }    
        
        void LoadProductData()
        {
            DataTable dt = CreateSampleProductData();        GridView1.DataSource = dt;
            GridView1.DataBind();
        }    #region sample data    static DataTable CreateSampleProductData()
        {
            DataTable tbl = new DataTable("Products");        tbl.Columns.Add("ProductID", typeof(int));
            tbl.Columns.Add("ProductName", typeof(string));        
            tbl.Columns.Add("UnitPrice", typeof(decimal));
            tbl.Columns.Add("CategoryID", typeof(int));        tbl.Rows.Add(1, "Chai", 18, 1);
            tbl.Rows.Add(2, "Chang", 19, 1);
            tbl.Rows.Add(3, "Aniseed Syrup", 10, 2);
            tbl.Rows.Add(4, "Chef Anton's Cajun Seasoning", 22, 2);
            tbl.Rows.Add(5, "Chef Anton's Gumbo Mix", 21.35, 2);
            tbl.Rows.Add(47, "Zaanse koeken", 9.5, 3);
            tbl.Rows.Add(48, "Chocolade", 12.75, 3);
            tbl.Rows.Add(49, "Maxilaku", 20, 3);                return tbl;
        }    #endregion    
          
    </script><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>CSDN_AccessGridViewRowByJS</title>
        <script type ="text/javascript">
        function getRowValue(sender)
        {   
            //debugger;
    //        if(sender.checked) {
                var tblRow = sender.parentNode.parentNode;
                // 改变 tblRow.cells[<cellIndex>] 中占位符 <cellIndex> 访问不同单元格
                return tblRow.cells[1].innerText + "," + tblRow.cells[2].innerText;
    //        }
        }
        
        function getAllRowValue(grdId, chkIdPart)
        {
            //debugger;
            var tbl = document.getElementById(grdId);
            var chkList;
            var txt = "";
            for(var i = 0; i < tbl.rows.length; i++) {
                chkList = tbl.rows[i].getElementsByTagName("input");
                for(var j = 0; j < chkList.length; j++) {
                    if(chkList[j].type == "checkbox" && chkList[j].checked && chkList[j].id.indexOf(chkIdPart) > -1) {
                        txt += getRowValue(chkList[j]) + "\n";                    
                    }
                }
            }
            return txt;
        }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input type="button" id="Button1" value="Rebind" onclick="location.href=location.href;" />
            <input type="button" id="Button2" value="Show All Checked Rows's Values" onclick="alert(getAllRowValue('GridView1', 'chkItem1'))" />
            <div style="float:left">
            <h1>GridView Version</h1>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
                <Columns>                              
                    <asp:TemplateField>
                        <ItemTemplate>                        
                            <asp:CheckBox ID="chkItem1" runat="server" onclick="if(this.checked) alert(getRowValue(this))" />
                            <%--OR--%>
                            <input type="checkbox" id="chkItem2" onclick="if(this.checked) alert(getRowValue(this))" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="ProductName" >
                        <ItemTemplate>
                            <%# Eval("ProductName") %>                        
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
                </Columns>
            </asp:GridView></div>
            </div>
        </form>
    </body>
    </html>
      

  11.   

    已发表一篇文章讨论此问题,附带代码详细解释以及测试效果图同时实现 GridView/DataGrid 两个版本ASP.NET DEMO 10: 如何通过 javascript 访问 GridView/DataGrid 选中 CheckBox 行各列的值  
    http://www.cnblogs.com/Jinglecat/archive/2007/07/15/818967.html
      

  12.   

    var oItem = spanChk.children;
    var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];
    xState=theBox.checked;
    elm=theBox.form.elements;
    for(i=0;i<elm.length;i++)
    if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)
    {
    if(elm[i].checked!=xState)
    elm[i].click();
    }
      

  13.   

       找了好久,已经用cs代码写过了。
    现在要改成js的。