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

高手接分
页面上两个控件CheckBox1,CheckBoxList1,当我选择CheckBoxList1的项时,CheckBox1的选择状态为非选,当选择CheckBox1时,CheckBoxList1的所有项都选择
马上揭贴!

------解决方案--------------------
<asp:CheckBox onclick= "javascript:GoSelect(this); " ID= "CheckBox1 " runat= "server " /> <asp:CheckBoxList ID= "CheckBoxList1 " runat= "server "> <asp:ListItem Value= "1 "> A </asp:ListItem> <asp:ListItem Value= "2 "> B </asp:ListItem> <asp:ListItem Value= "3 "> C </asp:ListItem> </asp:CheckBoxList> <script type= "text/javascript "> <!-- function GoSelect(obj) { var tb = document.getElementById( "CheckBoxList1 "); for(var i=0;i <tb.rows.length;i++) { tb.rows[i].cells[0].firstChild.checked = obj.checked; } } //--> </script>
------解决方案--------------------
借用cpp2017(慕白兄)的:
<asp:CheckBox onclick= "javascript:GoSelect(this); " ID= "CheckBox1 " runat= "server " />
<asp:CheckBoxList ID= "CheckBoxList1 " runat= "server ">
<asp:ListItem Value= "1 "> A </asp:ListItem>
<asp:ListItem Value= "2 "> B </asp:ListItem>
<asp:ListItem Value= "3 "> C </asp:ListItem>
</asp:CheckBoxList>
<script type= "text/javascript ">
<!--
function GoSelect(obj)
{
var tb = document.getElementById( "CheckBoxList1 ");
for(var i=0;i <tb.rows.length;i++)
{
tb.rows[i].cells[0].firstChild.checked = obj.checked;
}

}
function CheckSelect(obj)
{
var chk=true;
var tb = document.getElementById( "CheckBoxList1 ");
for(var i=0;i <tb.rows.length;i++)
{
if(!tb.rows[i].cells[0].firstChild.checked)
{
chk=false;
}
}
if(chk)document.getElementById( "CheckBox1 ").checked=true;
else document.getElementById( "CheckBox1 ").checked=false;
}

//-->
</script>
cs:
for(int i=0;i <CheckBoxList1.Items.Count;i++){
CheckBoxList1.Items[i].Attributes.Add( "onclick ", "CheckSelect(this) ");
}
------解决方案--------------------
完整代码
<%@ Page Language= "VB " AutoEventWireup= "false " CodeFile= "Default.aspx.vb " Inherits= "_Default " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
<script language=javascript>
function checkall(curobj)
{
var obj = document.getElementById( "CheckBoxList1 ");
var checklist = obj.getElementsByTagName( "INPUT ");
if(checklist)
{
for(var i=0;i <checklist.length;i++)
{
if(checklist[i].type== "checkbox ")
{
checklist[i].checked = curobj.checked;
}