使用 Jquery 在客户端 获取 CheckBoxList 绑定的value值 ,checkboxList绑定的Value值 是 ID
我要获取这个ID ,
如何实现 
在线等待!!

解决方案 »

  1.   

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
        <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
        <script type="text/javascript">
           $(document).ready(function(){
                $("a").click(function(){
                    $("#CheckBoxList1 :checkbox:checked").each(function(){
                        alert($(this).next().html())
                    })
                })
           })
        </script>
    </head>
    <body>
        <form id="Form1" 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:ListItem Value="4">d</asp:ListItem>
        </asp:CheckBoxList>
        <a>获取</a>
        </form>
    </body>
    </html>
      

  2.   

    var count=0;
    var container=document.getElementById("<%= CheckBoxList1.ClientID%>");
            var cbList=container.getElementsByTagName("input");
            for(var i=0;i<cbList.length;i++){
                if(cbList[i].type=="checkbox"&&cbList[i].checked){
                    count++;
                }
            }
      

  3.   

    能用 jquery 写个吗 
      

  4.   

    二楼就是jquery了
    <script type="text/javascript">
           $(document).ready(function(){
                $("a").click(function(){
                    $("#CheckBoxList1 :checkbox:checked").each(function(){
                        alert($(this).next().html())
                    })
                })
           })
        </script>
      

  5.   

    二楼的 只能获取到  a b c d 我呀获取 1 2 3 4
      

  6.   

    CheckBoxList结构如下:<asp:CheckBoxList ID="CheckBoxList1" runat="server">
                <asp:ListItem Value="1111">a</asp:ListItem>
                <asp:ListItem Value="2222">b</asp:ListItem>
                <asp:ListItem Value="3333">c</asp:ListItem>
                <asp:ListItem Value="44445">d</asp:ListItem>
            </asp:CheckBoxList>在正常情况下,CheckBoxList生成的html是不包含value属性的,请看以下生成的html源代码:<table id="CheckBoxList1" border="0">
    <tr>
    <td><input id="CheckBoxList1_0" type="checkbox" name="CheckBoxList1$0" /><label for="CheckBoxList1_0">a</label></td>
    </tr><tr>
    <td><input id="CheckBoxList1_1" type="checkbox" name="CheckBoxList1$1" /><label for="CheckBoxList1_1">b</label></td>
    </tr><tr>
    <td><input id="CheckBoxList1_2" type="checkbox" name="CheckBoxList1$2" /><label for="CheckBoxList1_2">c</label></td>
    </tr><tr>
    <td><input id="CheckBoxList1_3" type="checkbox" name="CheckBoxList1$3" /><label for="CheckBoxList1_3">d</label></td>
    </tr>
    所以,必须想办法把ListItem的Value的值放到生成的html代码中,以下是1种思路,手动把Value值放到html元素中,见以下代码:
    using System;
    using System.Web.UI.WebControls;public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            foreach (ListItem item in CheckBoxList1.Items)
            {
                item.Attributes.Add("val", item.Value);
            }
        }
    }
    这里再运行页面,看看生成的html是怎样的:<table id="CheckBoxList1" border="0">
    <tr>
    <td><span val="1111"><input id="CheckBoxList1_0" type="checkbox" name="CheckBoxList1$0" /><label for="CheckBoxList1_0">a</label></span></td>
    </tr><tr>
    <td><span val="2222"><input id="CheckBoxList1_1" type="checkbox" name="CheckBoxList1$1" /><label for="CheckBoxList1_1">b</label></span></td>
    </tr><tr>
    <td><span val="3333"><input id="CheckBoxList1_2" type="checkbox" name="CheckBoxList1$2" /><label for="CheckBoxList1_2">c</label></span></td>
    </tr><tr>
    <td><span val="44445"><input id="CheckBoxList1_3" type="checkbox" name="CheckBoxList1$3" /><label for="CheckBoxList1_3">d</label></span></td>
    </tr>
    哈哈,似乎有希望了,在每个input元素都有一个span标签包裹着,其属性val的值就是我们想要的。完整的aspx代码:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!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 type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(function() {
                $("a").click(function() {
                    $("#CheckBoxList1 :checkbox:checked").each(function() {
                        alert($(this).parent('span').attr('val'));
                    });
                })
            })
        </script></head>
    <body>
        <form id="form1" runat="server">
            <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                <asp:ListItem Value="1111">a</asp:ListItem>
                <asp:ListItem Value="2222">b</asp:ListItem>
                <asp:ListItem Value="3333">c</asp:ListItem>
                <asp:ListItem Value="44445">d</asp:ListItem>
            </asp:CheckBoxList>
            <a href="javascript:void(0);">获取</a>
        </form>
    </body>
    </html>完整的cs代码:using System;
    using System.Web.UI.WebControls;public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            foreach (ListItem item in CheckBoxList1.Items)
            {
                item.Attributes.Add("val", item.Value);
            }
        }
    }
    这里还有另一种思路,扩展CheckBoxList控件,若CheckBoxList用的很多,推荐这种做法,更加符合OO原则。
    http://www.cnblogs.com/johnwonder/archive/2010/03/02/1676525.html
      

  7.   

    把$(this).next().val()这段话改成$(this).next().val()