当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript获得CheckBoxList选中的数量

Javascript
web开发设计师比较费解的JavaScript
jQuery教程:整理的几个常见的初学者问题
免费资源:7个效果非常棒的jQuery 3D效果插件
JavaScript教程:编写匿名函数的几种方法
jQuery教程:jQuery的核心
jQuery教程:jQuery核心方法的使用
webjx收集45个jQuery导航插件和教程
30个气泡悬浮框(Tooltip)的jQuery插件
Jetpack扩展案例:Gmail邮件提醒功能
非常出色的jQuery运动特效可以和Flash媲美
ImagesLazyLoad 图片延迟加载效果
收集国外的14个图片放大编辑的jQuery插件
修改和创建DOM节点两种方式的4种优化方案
jQuery.Switchable整合插件用途介绍
提高Textarea操作性能优秀的jQuery插件
WEBJX收集12个非常有创意的JavaScript小游戏
Javascript教程:关于深入了解JS的几个问题

Javascript 中的 javascript获得CheckBoxList选中的数量


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 126 ::
收藏到网摘: n/a

javascript获得CheckBoxList选中的数量(jQuery与Javascript对照学习/前台与后台) jQuery的选择器真的好强大,好灵活。 javascript的原始方法也值得研究。
复制代码 代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckBoxList.aspx.cs" Inherits="CheckBoxList" %>
<!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>获得CheckBoxList选中的数量(jQuery与Javascript对照学习/前台与后台)</title>
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
//jQuery的方法(王君)
$(function(){
$("#chkBox").click(function(){
alert($("#chkBox input[@type=checkbox]:checked").size());
});
});
//javacript方法(候林)
function f(){
var a=document.getElementsByTagName('input')
var num=0;
for(var i=0;i<a.length;i++){
if(a[i].type=='checkbox'){
if(a[i].checked==true)
num+=1;
}
}
alert(num);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
jQuery的选择器真的好强大,好灵活。<br />
javascript的原始方法也值得研究。
</div>
<div>
<input type="button" value="Javascript取值" onclick="f();" />
<asp:CheckBoxList ID="chkBox" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="btnOk" runat="server" OnClick="btnOk_Click" Text="服务器端取" />
</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 CheckBoxList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnOk_Click(object sender, EventArgs e)
{
int totalNum = 0;//总数
string list = "000";//选中的值
for (int i = 0; i < this.chkBox.Items.Count; i++)
{
if (chkBox.Items[i].Selected)
{
totalNum += 1;
list += "," + chkBox.Items[i].Value;
}
}
Response.Write(totalNum.ToString() + "|" + list);
}
}