[英]Check if asp Checkbox is checked in a row of a GridView with Javascript
I have a grid view and I want to see if the checkbox in the first column is checked. 我有一个网格视图,我想看看第一栏中的复选框是否被选中。 If the check box is checked it opens a new window. 如果选中此复选框,则会打开一个新窗口。 I cannot figure out how to see if the checkbox is checked. 我不知道如何查看该复选框是否已选中。 Please help the function below is not working and I can't figure out why. 请帮助下面的功能无法正常工作,我不知道为什么。
function mapSelectedClick()
{
var CustomerIDs = "";
var grid = document.getElementById('<%=grdCustomers.ClientID %>');
for (var i = 1; i < grid.rows.length; i++)
{
var Row = grid.rows[i];
var CustomerID = grid.rows[i].cells[1].innerText;
if (grid.rows[i].cell[0].type == "checkbox")
{
if (grid.rows[i].cell[0].childNodes[0].checked)
{
customerIDs += CustomerID.toString() + ',';
}
}
}
customerIDs = customerIDs.substring(0, customerIDs.length-1);
window.open("MapCustomers.aspx?CustomerIDs=" + customerIDs);
}
There are few problems in your code: 您的代码中很少有问题:
if
conditions, you used cell[0]
instead of cells[0]
在两种if
下,您都使用cell[0]
而不是cells[0]
if
condition you used cell[0].type == "checkbox"
. 在外部if
条件中,您使用了cell[0].type == "checkbox"
。 Cell can't have type checkbox, it includes chekbox
control as its child 单元格不能具有类型复选框,它包括chekbox
控件作为其子级 Modify your function like this: 像这样修改您的功能:
function mapSelectedClick()
{
var CustomerIDs = "";
var grid = document.getElementById('<%=grdCustomers.ClientID %>');
for (var i = 1; i < grid.rows.length; i++)
{
var Row = grid.rows[i];
var CustomerID = Row.cells[1].innerText;
var ctrl = Row.cells[0].childNodes[0];
if (ctrl.type == "checkbox")
{
if (ctrl.checked)
{
customerIDs += CustomerID.toString() + ',';
}
}
}
customerIDs = customerIDs.substring(0, customerIDs.length-1);
window.open("MapCustomers.aspx?CustomerIDs=" + customerIDs);
}
Note that innerText
is not cross browser compatible, use innerHTML wherever possible. 请注意, innerText
与跨浏览器不兼容,请尽可能使用innerHTML。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.