javascript獲得CheckBoxList選中的數(shù)量
更新時間:2009年10月27日 23:41:59 作者:
javascript獲得CheckBoxList選中的數(shù)量(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選中的數(shù)量(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;//總數(shù)
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);
}
}
復制代碼 代碼如下:
<%@ 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選中的數(shù)量(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;//總數(shù)
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);
}
}
您可能感興趣的文章:
- javascript基于jQuery的表格懸停變色/恢復,表格點擊變色/恢復,點擊行選Checkbox
- 選擇指定數(shù)量后checkbox不可選(變灰)javascript代碼
- 用 Javascript 驗證表單(form)中多選框(checkbox)值
- 利用JavaScript更改input中radio和checkbox樣式
- IE7中javascript操作CheckBox的checked=true不打勾的解決方法
- asp.net Javascript獲取CheckBoxList的value
- Javascript 實現(xiàn)TreeView CheckBox全選效果
- javaScript checkbox 全選/反選及批量刪除
- Javascript實現(xiàn)CheckBox的全選與取消全選的代碼
- 用Javascript讀取CheckBox數(shù)組的值的代碼(兼容IE與firefox)
- 驗證用戶必選CheckBox控件與自定義驗證javascript代碼
- javascript中checkbox使用方法簡單實例演示
- javascript中checkbox使用方法實例演示
相關文章
javascript 中的console.log和彈出窗口alert
這篇文章主要介紹了javascript 中的console.log和彈出窗口alert 的相關資料,非常不錯,具有參考借鑒價值,感興趣的朋友參考下吧2016-08-08如何理解JS函數(shù)防抖和函數(shù)節(jié)流
函數(shù)防抖和函數(shù)節(jié)流都是對函數(shù)進行特殊的設置,減少該函數(shù)在某一時間段內頻繁觸發(fā)帶來的副作用。二者只是采用的設置方式和原理不一樣,其最終的目的是一樣的。2021-05-05