日期:2014-05-18  浏览次数:20471 次

求一段javascript代码
在DataGrid中,左边一列是CheckBox,如果勾选标题栏中的CheckBox,则这一列的所有CheckBox全部选中,在点击一次,全部取消,请问怎么写。

<asp:datagrid   id= "DataGrid1 "   runat= "server "   >
<Columns>
<asp:BoundColumn   DataField= "UserID "   SortExpression= "UserID "> </asp:BoundColumn>
<asp:BoundColumn   DataField= "UserName "   SortExpression= "UserName "> </asp:BoundColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<INPUT   type= "checkbox "   name= "chkUnItems1 "   id= "chkUnItems1 "   onclick= "SetItemAll1() ">
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox   id= "chkSelected "   runat= "server "> </asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid> &nbsp;

------解决方案--------------------
function SetItemAll1(obj)
{
for(var i=0;i <form.elements.length;i++)
{
form.elements[i].checked = obj.checked;
}
}
<INPUT type= "checkbox " name= "chkUnItems1 " id= "chkUnItems1 " onclick= "SetItemAll1(this) ">

------解决方案--------------------
var checkFlag = true;
function ChooseAll()
{

if( checkFlag ) // 全选 
{
var inputs = document.all.tags( "INPUT ");
for (var i=0; i < inputs.length; i++) // 遍历页面上所有的 input
{
//&& (inputs[i].name.substring(inputs[i].name.length - 9, inputs[i].name.length) == "chkSelect ")
if (inputs[i].type == "checkbox " && inputs[i].value != "CheckBoxAll " )
{
inputs[i].checked = true;
}
}
checkFlag = false;
}
else// 取消全选
{
var inputs = document.all.tags( "INPUT ");
for (var i=0; i < inputs.length; i++) // 遍历页面上所有的 input
{
if (inputs[i].type == "checkbox " && inputs[i].value != "CheckBoxAll " )
{
inputs[i].checked = false;
}
}
checkFlag = true;
}
}