DoubleM

我的工作是:需求分析、用户体验、项目管理、类设计、技术方向选择、解决复杂的技术问题。 这些领域的事情相对比较轻松,所以也经常有闲写写代码,不过多以折磨自己的大脑为目标,兴趣使然。
  新随笔  :: 订阅 订阅  :: 管理

JS遍历页面上的checkbox

Posted on 2011-08-09 13:17  DoubleMM  阅读(3144)  评论(0编辑  收藏  举报

<%@ Page Language="C#" EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected
void Button1_Click(object sender, EventArgs e)
{
  FindBox(
this.Controls);
}

private
void FindBox(ControlCollection cc)
{
foreach (Control c
in cc)
{
  
if (c.HasControls())
   {
     FindBox(c.Controls);
   }
  
if (c is CheckBox)
   {
     CheckBox x
= c as CheckBox;
    
//判断名称
    if (x.Attributes["name"] != null && x.Attributes["name"].Equals("Admin_Power"))
      {
        Response.Write(
"<li>" + x.Attributes["name"]);
      }
   }
 }
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<form runat="server" id="form1">
<asp:CheckBox ID="CheckBox7" runat="server" name="xxxxx" Text=" xxxxx" />
<asp:CheckBox ID="CheckBox1" runat="server" name="Admin_Power" Text=" 投票1" />
<asp:CheckBox ID="CheckBox2" runat="server" name="Admin_Power" Text=" 投票2" />
<asp:CheckBox ID="CheckBox3" runat="server" name="Admin_Power" Text=" 投票3" />
<asp:CheckBox ID="CheckBox4" runat="server" name="Admin_Power" Text=" 投票4" />
<asp:CheckBox ID="CheckBox5" runat="server" name="Admin_Power" Text=" 投票5" />
<asp:CheckBox ID="CheckBox6" runat="server" name="Admin_Power" Text=" 投票6" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="得到" />
</form>
</body>
</html>