<script type="text/javascript">
$(document).ready(function() {
$("#btnShow").click(function() {
var valuelist = ""; //保存checkbox选中值
var valueId="";
var value="";
//遍历name以listTest开头的checkbox
$("input[name^='CBColor']").each(function() {
if ($(this).is(":checked")) {
//$(this):当前checkbox对象;
//$(this).parent("span"):checkbox父级span对象
valueId=$(this).parent("span").attr("colorId");
valuelist = $(this).next().text();
value+=valueId+":"+valuelist;
}
});
if (valuelist.length > 0) {
alert(value);
}
});
});
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="CBColor" runat="server">
</asp:CheckBoxList>
<asp:Button ID="btnShow" runat="server" Text="Button" />
</div>
</form>
</body>
后台程序:
//获取颜色的数据
public DataTable GetColor()
{
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
SqlConnection con = new SqlConnection(conStr);
SqlCommand com = new SqlCommand("select * from color",con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public void GetColorFromBLL()
{
DataTable table = GetColor();
foreach (DataRow item in table.Rows)
{
ListItem listItem = new ListItem(item["colorName"].ToString(), item["cid"].ToString());
listItem.Attributes.Add("colorId", item["cid"].ToString());
this.CBColor.Items.Add(listItem);
}
this.CBColor.DataBind();
}