<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckBoxListValue.aspx.cs" Inherits="test_CheckBoxListValue" %>
<!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>Javascript得到CheckBoxList的Value</title>
<script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
//把选中的值写入hiddenfield
function f(){
var total = $("#chkBox input[@type=checkbox]:checked").size();
var checkValues = '';
$("#chkBox input[@type=checkbox]:checked").each(function(){
checkValues += $(this).parent().attr("checkValue") + ",";
});
alert("选中了" + total + "个" + "\n" + "值:" + checkValues.substring(0,checkValues.length-1));
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
主要思路就是为ListItem加一个"checkValue"属性,前台通过javascript找到。
</div>
<div>
<input type="button" onclick="f();" value="取值" />
<asp:CheckBoxList ID="chkBox" runat="server">
</asp:CheckBoxList>
</div>
</form>
</body>
</html>
<!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>Javascript得到CheckBoxList的Value</title>
<script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
//把选中的值写入hiddenfield
function f(){
var total = $("#chkBox input[@type=checkbox]:checked").size();
var checkValues = '';
$("#chkBox input[@type=checkbox]:checked").each(function(){
checkValues += $(this).parent().attr("checkValue") + ",";
});
alert("选中了" + total + "个" + "\n" + "值:" + checkValues.substring(0,checkValues.length-1));
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
主要思路就是为ListItem加一个"checkValue"属性,前台通过javascript找到。
</div>
<div>
<input type="button" onclick="f();" value="取值" />
<asp:CheckBoxList ID="chkBox" runat="server">
</asp:CheckBoxList>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class test_CheckBoxListValue : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Bind();
}
private void Bind()
{
ListItem li1 = new ListItem("1", "1");
li1.Attributes.Add("checkValue", "1");
chkBox.Items.Add(li1);
ListItem li2 = new ListItem("2", "2");
li2.Attributes.Add("checkValue", "2");
chkBox.Items.Add(li2);
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class test_CheckBoxListValue : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Bind();
}
private void Bind()
{
ListItem li1 = new ListItem("1", "1");
li1.Attributes.Add("checkValue", "1");
chkBox.Items.Add(li1);
ListItem li2 = new ListItem("2", "2");
li2.Attributes.Add("checkValue", "2");
chkBox.Items.Add(li2);
}
}