function getQuestionID(grvid)
{
var o_grv = document.getElementById(grvid);
var ret = "";
for(var i = 1; i < o_grv.rows.length; i++)
{
var o_qid = o_grv.rows[i].cells[0].children[0].id;//问题出现在这一句 var o_tb2 = document.getElementById(o_qid);
var o_cid = o_tb2.rows[0].cells[0].children[2].id;
alert(i+"|"+o_qid+"|"+o_cid); var o_c = document.getElementById(o_cid);
ret += o_c.innerHTML;
ret += ",";
} return ret.substring(0, ret.length - 1);
}我用上述代码遍历GridView,想获取每行的一个控制值,但我不能正确获取行中子控制的ID,问题出现在这一句:
var o_qid = o_grv.rows[i].cells[0].children[0].id;//问题出现在这一句
无论行号i如何改变,o_qid 总是取到你1行(行1)的子控制的ID值。
请教如何遍历GridView获取行中控制的ID?
<table cellspacing="0" rules="all" border="1" id="GridView1" style="width:100%;border-collapse:collapse;">
<tr align="left" style="font-size:12pt;">
<th scope="col">一、选择题</th>
</tr><tr>
<td>
<table id="Table2" cellspacing="1" cellpadding="1" width="100%" align="center" border="0">
<tr>
<td colspan="2">
<span id="GridView1_ctl02_Label1">1</span>
<span id="GridView1_ctl02_Label2">、1国内最专业的Asp.net源码下载网站是?</span>
<span id="GridView1_ctl02_Label3" style="display:none">1</span>
</td>
<td colspan="1" style="text-align: right">
<input id="BtnDelQuestionSingle" type="button" value="删除本题" onclick="return BtnDelQuestionSingle_onclick(this)" /></td>
</tr>
<tr>
<td width="35%">
<input id="GridView1_ctl02_RadioButton1" type="radio" name="GridView1$ctl02$Sl" value="RadioButton1" /><label for="GridView1_ctl02_RadioButton1">csdn.net</label></td>
<td width="35%">
<input id="GridView1_ctl02_RadioButton2" type="radio" name="GridView1$ctl02$Sl" value="RadioButton2" /><label for="GridView1_ctl02_RadioButton2">微软Asp.net</label></td>
<td>
</td>
</tr>
<tr>
<td width="35%">
<input id="GridView1_ctl02_RadioButton3" type="radio" name="GridView1$ctl02$Sl" value="RadioButton3" /><label for="GridView1_ctl02_RadioButton3">博客园</label></td>
<td width="35%">
<input id="GridView1_ctl02_RadioButton4" type="radio" name="GridView1$ctl02$Sl" value="RadioButton4" /><label for="GridView1_ctl02_RadioButton4">51aspx</label></td>
<td>
</td>
</tr>
</table>
</td>
</tr><tr>
<td>
<table id="Table1" cellspacing="1" cellpadding="1" width="100%" align="center" border="0">
<tr>
<td colspan="2">
<span id="GridView1_ctl03_Label1">2</span>
<span id="GridView1_ctl03_Label2">、2国内最专业的Asp.net源码下载网站是?</span>
<span id="GridView1_ctl03_Label3" style="display:none">37</span>
</td>
<td colspan="1" style="text-align: right">
<input id="Button1" type="button" value="删除本题" onclick="return BtnDelQuestionSingle_onclick(this)" /></td>
</tr>
<tr>
<td width="35%">
<input id="GridView1_ctl03_RadioButton1" type="radio" name="GridView1$ctl03$Sl" value="RadioButton1" /><label for="GridView1_ctl03_RadioButton1">csdn.net</label></td>
<td width="35%">
<input id="GridView1_ctl03_RadioButton2" type="radio" name="GridView1$ctl03$Sl" value="RadioButton2" /><label for="GridView1_ctl03_RadioButton2">微软Asp.net</label></td>
<td>
</td>
</tr>
<tr>
<td width="35%">
<input id="GridView1_ctl03_RadioButton3" type="radio" name="GridView1$ctl03$Sl" value="RadioButton3" /><label for="GridView1_ctl03_RadioButton3">博客园</label></td>
<td width="35%">
<input id="GridView1_ctl03_RadioButton4" type="radio" name="GridView1$ctl03$Sl" value="RadioButton4" /><label for="GridView1_ctl03_RadioButton4">51aspx</label></td>
<td>
</td>
</tr>
</table>
</td>
</tr><tr>
<td>
<table id="Table3" cellspacing="1" cellpadding="1" width="100%" align="center" border="0">
<tr>
<td colspan="2">
<span id="GridView1_ctl04_Label1">3</span>
<span id="GridView1_ctl04_Label2">、3国内最专业的Asp.net源码下载网站是?</span>
<span id="GridView1_ctl04_Label3" style="display:none">38</span>
</td>
<td colspan="1" style="text-align: right">
<input id="Button2" type="button" value="删除本题" onclick="return BtnDelQuestionSingle_onclick(this)" /></td>
</tr>
<tr>
<td width="35%">
<input id="GridView1_ctl04_RadioButton1" type="radio" name="GridView1$ctl04$Sl" value="RadioButton1" /><label for="GridView1_ctl04_RadioButton1">csdn.net</label></td>
<td width="35%">
<input id="GridView1_ctl04_RadioButton2" type="radio" name="GridView1$ctl04$Sl" value="RadioButton2" /><label for="GridView1_ctl04_RadioButton2">微软Asp.net</label></td>
<td>
</td>
</tr>
<tr>
<td width="35%">
<input id="GridView1_ctl04_RadioButton3" type="radio" name="GridView1$ctl04$Sl" value="RadioButton3" /><label for="GridView1_ctl04_RadioButton3">博客园</label></td>
<td width="35%">
<input id="GridView1_ctl04_RadioButton4" type="radio" name="GridView1$ctl04$Sl" value="RadioButton4" /><label for="GridView1_ctl04_RadioButton4">51aspx</label></td>
<td>
</td>
</tr>
</table>
</td>
</tr>
</table>
这是前台代码
{
var o_grv = document.getElementById(grvid);
var ret = "";
for(var i = 1; i < o_grv.rows.length; i++)
{
var o_qid = o_grv.rows[i].cells[0].children[0].id;//问题出现在这一句 var o_tb2 = document.getElementById(o_qid);
var o_cid = o_tb2.rows[0].cells[0].children[2].id;
alert(i+"|"+o_qid+"|"+o_cid); var o_c = document.getElementById(o_cid);
ret += o_c.innerHTML;
ret += ",";
} return ret.substring(0, ret.length - 1);
}我用上述代码遍历GridView,想获取每行的一个控制值,但我不能正确获取行中子控制的ID,问题出现在这一句:
var o_qid = o_grv.rows[i].cells[0].children[0].id;//问题出现在这一句
无论行号i如何改变,o_qid 总是取到你1行(行1)的子控制的ID值。
请教如何遍历GridView获取行中控制的ID?
<table cellspacing="0" rules="all" border="1" id="GridView1" style="width:100%;border-collapse:collapse;">
<tr align="left" style="font-size:12pt;">
<th scope="col">一、选择题</th>
</tr><tr>
<td>
<table id="Table2" cellspacing="1" cellpadding="1" width="100%" align="center" border="0">
<tr>
<td colspan="2">
<span id="GridView1_ctl02_Label1">1</span>
<span id="GridView1_ctl02_Label2">、1国内最专业的Asp.net源码下载网站是?</span>
<span id="GridView1_ctl02_Label3" style="display:none">1</span>
</td>
<td colspan="1" style="text-align: right">
<input id="BtnDelQuestionSingle" type="button" value="删除本题" onclick="return BtnDelQuestionSingle_onclick(this)" /></td>
</tr>
<tr>
<td width="35%">
<input id="GridView1_ctl02_RadioButton1" type="radio" name="GridView1$ctl02$Sl" value="RadioButton1" /><label for="GridView1_ctl02_RadioButton1">csdn.net</label></td>
<td width="35%">
<input id="GridView1_ctl02_RadioButton2" type="radio" name="GridView1$ctl02$Sl" value="RadioButton2" /><label for="GridView1_ctl02_RadioButton2">微软Asp.net</label></td>
<td>
</td>
</tr>
<tr>
<td width="35%">
<input id="GridView1_ctl02_RadioButton3" type="radio" name="GridView1$ctl02$Sl" value="RadioButton3" /><label for="GridView1_ctl02_RadioButton3">博客园</label></td>
<td width="35%">
<input id="GridView1_ctl02_RadioButton4" type="radio" name="GridView1$ctl02$Sl" value="RadioButton4" /><label for="GridView1_ctl02_RadioButton4">51aspx</label></td>
<td>
</td>
</tr>
</table>
</td>
</tr><tr>
<td>
<table id="Table1" cellspacing="1" cellpadding="1" width="100%" align="center" border="0">
<tr>
<td colspan="2">
<span id="GridView1_ctl03_Label1">2</span>
<span id="GridView1_ctl03_Label2">、2国内最专业的Asp.net源码下载网站是?</span>
<span id="GridView1_ctl03_Label3" style="display:none">37</span>
</td>
<td colspan="1" style="text-align: right">
<input id="Button1" type="button" value="删除本题" onclick="return BtnDelQuestionSingle_onclick(this)" /></td>
</tr>
<tr>
<td width="35%">
<input id="GridView1_ctl03_RadioButton1" type="radio" name="GridView1$ctl03$Sl" value="RadioButton1" /><label for="GridView1_ctl03_RadioButton1">csdn.net</label></td>
<td width="35%">
<input id="GridView1_ctl03_RadioButton2" type="radio" name="GridView1$ctl03$Sl" value="RadioButton2" /><label for="GridView1_ctl03_RadioButton2">微软Asp.net</label></td>
<td>
</td>
</tr>
<tr>
<td width="35%">
<input id="GridView1_ctl03_RadioButton3" type="radio" name="GridView1$ctl03$Sl" value="RadioButton3" /><label for="GridView1_ctl03_RadioButton3">博客园</label></td>
<td width="35%">
<input id="GridView1_ctl03_RadioButton4" type="radio" name="GridView1$ctl03$Sl" value="RadioButton4" /><label for="GridView1_ctl03_RadioButton4">51aspx</label></td>
<td>
</td>
</tr>
</table>
</td>
</tr><tr>
<td>
<table id="Table3" cellspacing="1" cellpadding="1" width="100%" align="center" border="0">
<tr>
<td colspan="2">
<span id="GridView1_ctl04_Label1">3</span>
<span id="GridView1_ctl04_Label2">、3国内最专业的Asp.net源码下载网站是?</span>
<span id="GridView1_ctl04_Label3" style="display:none">38</span>
</td>
<td colspan="1" style="text-align: right">
<input id="Button2" type="button" value="删除本题" onclick="return BtnDelQuestionSingle_onclick(this)" /></td>
</tr>
<tr>
<td width="35%">
<input id="GridView1_ctl04_RadioButton1" type="radio" name="GridView1$ctl04$Sl" value="RadioButton1" /><label for="GridView1_ctl04_RadioButton1">csdn.net</label></td>
<td width="35%">
<input id="GridView1_ctl04_RadioButton2" type="radio" name="GridView1$ctl04$Sl" value="RadioButton2" /><label for="GridView1_ctl04_RadioButton2">微软Asp.net</label></td>
<td>
</td>
</tr>
<tr>
<td width="35%">
<input id="GridView1_ctl04_RadioButton3" type="radio" name="GridView1$ctl04$Sl" value="RadioButton3" /><label for="GridView1_ctl04_RadioButton3">博客园</label></td>
<td width="35%">
<input id="GridView1_ctl04_RadioButton4" type="radio" name="GridView1$ctl04$Sl" value="RadioButton4" /><label for="GridView1_ctl04_RadioButton4">51aspx</label></td>
<td>
</td>
</tr>
</table>
</td>
</tr>
</table>
这是前台代码
如果是子控件的话,试试用findcontrol()函数试试。。