这是在GridView里面没有控件的情况下,有控件另当别论 ;
var gdview=<%=gridview.ClientID%>//gridview是GridView控件的ID
或者
var gdview=document.getElementById("<%=gridview.ClientID %>")
//1 读取指定行单元格的值如果是0则读出其表头的值
//rowIndex=1就是第一行的值
function getCellValueByRow(rowIndex)
{
for(var i=0i<gdview.rows(rowIndex).cells.lengthi++)
{
alert(gdview.rows(rowIndex).cells(i).innerText)//弹出指定行单元格的值
}
}
//2 读取指定列单元格的值 cellIndex=0则为第一列的值
function getValueByCell(cellIndex)
{
for(var i=0i<gdview.rows.lengthi++)
{
alert(gdview.rows(i).cells(cellIndex).innerText)//指定列单元格的值
}
}
//3 读取所有单元格的值
function getValueAll(cellIndex)
{
for(var i=1i<gdview.rows.lengthi++)//要从1开始,从0则会读出表头的值
{
for(varj=0j<gdview.rows(i).cells.lengthj++)
{
alert(gdview.rows(i).cells(j).innerText)//读取所有单元格的值
}
}
}
//4 读取列标头的值
functioin getValueTitle()
{
for(var i=0i<gdview.rows(0).cells.lengthi++)
{
alert(gdview.rows(0).cells(i).innerText)//读取列标头的值
}
}
1.直接运行次,然后用察看源代码2.找到你想控制的元素的ID,用documen.getElementById(ID),就可以找到了
理论上这个ID,应该很长。
如果你想循环得到,我JS就不会了,我都用JQuery做的。原理一样
依然是上面的步骤,然后,一层层往下循环。
希望对你有帮助。
用的是asp.net,GridView实现它的RowDataBound事件,具体看代码,仅供参考:前台:
<html xmlns="
<head runat="server">
<title></title>
<!--点击GridView任意行的位置,获取该行的ID值-->
<script type="text/javascript">
function getId(row_index) {
var grid_view = document.getElementById('<%=GridView1.ClientID %>')
var rows = grid_view.rows
var personID = rows[row_index].cells[0].innerHTML
alert("获取的ID为:" + personID)
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" HeaderStyle-Width="300px" ItemStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="LightSkyBlue" />
<asp:BoundField HeaderText="Name" DataField="Name" HeaderStyle-Width="300px" ItemStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="LightSkyBlue" />
<asp:BoundField HeaderText="Tel" DataField="Tel" HeaderStyle-Width="300px" ItemStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="LightSkyBlue" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
后台代码:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Person> list = new List<Person>()
list.Add(new Person("001", "张三"))
list.Add(new Person("002", "李四"))
list.Add(new Person("003", "王五"))
list.Add(new Person("004", "赵六"))
list.Add(new Person("005", "何七"))
GridView1.DataSource = list
GridView1.DataBind()
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int row_index = e.Row.RowIndex + 1
e.Row.Attributes.Add("onclick", "getId(" + row_index + ")")
}
}
}
class Person
{
public Person(string id, string name)
{
ID = id
Name = name
}
private string id
public string ID
{
get { return id }
set { id = value }
}
private string name
public string Name
{
get { return name }
set { name = value }
}
private string tel
public string Tel
{
get { return tel }
set { tel = value }
}
}