关于 Jquery获取 checkboxList 多选框中绑定的Value值 使用 Jquery 在客户端 获取 CheckBoxList 绑定的value值 ,checkboxList绑定的Value值 是 ID我要获取这个ID ,如何实现 在线等待!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <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> 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++; } } 能用 jquery 写个吗 二楼就是jquery了<script type="text/javascript"> $(document).ready(function(){ $("a").click(function(){ $("#CheckBoxList1 :checkbox:checked").each(function(){ alert($(this).next().html()) }) }) }) </script> 二楼的 只能获取到 a b c d 我呀获取 1 2 3 4 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 把$(this).next().val()这段话改成$(this).next().val() 请教~adRotator控件不显示图片! 用DataSet查询后显示,急! 刷新的小问题 在ASP.NET中怎么判断是否查询到相关记录 访问局域网其他电脑的共享文件夹,总是提示"不明的使用者名稱或錯誤密碼" 如何设置datagrid列的格式???? 如何在后台类中直接接受confirm及prompt返回的结果? 请问一下,我这段语句错在那里 如何在DataGrid控件中每一行第一列加入象项目列表那样的图片? 请问可以用asp.net bata2版做应用程序吗?他有什么限制甚至后患吗? 是否能让其它网站使用我网站的图片变成文字链接? 【百分悬赏】ASP.NET中如何实现这种图片上传?
<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>
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++;
}
}
<script type="text/javascript">
$(document).ready(function(){
$("a").click(function(){
$("#CheckBoxList1 :checkbox:checked").each(function(){
alert($(this).next().html())
})
})
})
</script>
<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