jquery統(tǒng)計(jì)復(fù)選框選中示例
更新時(shí)間:2013年11月05日 17:19:04 作者:
在jquery中如果我們要獲取選中的checkbox我們可以input:checked,然后獲取選中checkbox個(gè)數(shù)量時(shí)我們可以使用jQuery的size()方法或length屬性來判斷就行了
以前我使用js只能判斷遍歷再獲取
<!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>
<title>jQuery判斷復(fù)選框的選中個(gè)數(shù)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
window.onload = function(){//頁(yè)面所有元素加載完畢
var btn = document.getElementById("btn"); //獲取id為btn的元素(button)
btn.onclick = function(){ //給元素添加onclick事件
var arrays = new Array(); //創(chuàng)建一個(gè)數(shù)組對(duì)象
var items = document.getElementsByName("check"); //獲取name為check的一組元素(checkbox)
for(i=0; i < items.length; i++){ //循環(huán)這組數(shù)據(jù)
if(items[i].checked){ //判斷是否選中
arrays.push(items[i].value); //把符合條件的 添加到數(shù)組中. push()是javascript數(shù)組中的方法.
}
}
alert( "選中的個(gè)數(shù)為:"+arrays.length );
}
}
</script>
</head>
<body>
<form method="post" action="#">
<input type="checkbox" value="1" name="check" checked="checked"/>
<input type="checkbox" value="2" name="check" />
<input type="checkbox" value="3" name="check" checked="checked"/>
<input type="button" value="你選中的個(gè)數(shù)" id="btn"/>
</form>
</body>
</html>
通過jQuery獲取checkbox選中項(xiàng)的個(gè)數(shù),需要用到j(luò)Query的size()方法或length屬性,下面的例子是通過length屬性獲得checkbox選中項(xiàng)的個(gè)數(shù)。
<script Language="JScript">
function check(){
var boxArray = document.getElementsByName('oBox');
var total = 0;
for(var i=0;i<boxArray.length;i++){
if(boxArray[i].checked){
total++;
}
}
if(total>0){
if(window.confirm('共選中'+total+'首歌,是否繼續(xù)?')){
window.open('about:blank','SubWin','');
return true;
}
else{
return false;
}
}
else{
window.alert('沒有選擇!') ;
return false;
}
}
</script>
<form target="SubWin" onsubmit="return check();">
<input type="checkbox" name="oBox">歌曲一
<input type="checkbox" name="oBox">歌曲二
<input type="checkbox" name="oBox">歌曲三
<input type="button" value="看看">
</form>
復(fù)制代碼 代碼如下:
<!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>
<title>jQuery判斷復(fù)選框的選中個(gè)數(shù)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
window.onload = function(){//頁(yè)面所有元素加載完畢
var btn = document.getElementById("btn"); //獲取id為btn的元素(button)
btn.onclick = function(){ //給元素添加onclick事件
var arrays = new Array(); //創(chuàng)建一個(gè)數(shù)組對(duì)象
var items = document.getElementsByName("check"); //獲取name為check的一組元素(checkbox)
for(i=0; i < items.length; i++){ //循環(huán)這組數(shù)據(jù)
if(items[i].checked){ //判斷是否選中
arrays.push(items[i].value); //把符合條件的 添加到數(shù)組中. push()是javascript數(shù)組中的方法.
}
}
alert( "選中的個(gè)數(shù)為:"+arrays.length );
}
}
</script>
</head>
<body>
<form method="post" action="#">
<input type="checkbox" value="1" name="check" checked="checked"/>
<input type="checkbox" value="2" name="check" />
<input type="checkbox" value="3" name="check" checked="checked"/>
<input type="button" value="你選中的個(gè)數(shù)" id="btn"/>
</form>
</body>
</html>
通過jQuery獲取checkbox選中項(xiàng)的個(gè)數(shù),需要用到j(luò)Query的size()方法或length屬性,下面的例子是通過length屬性獲得checkbox選中項(xiàng)的個(gè)數(shù)。
復(fù)制代碼 代碼如下:
<script Language="JScript">
function check(){
var boxArray = document.getElementsByName('oBox');
var total = 0;
for(var i=0;i<boxArray.length;i++){
if(boxArray[i].checked){
total++;
}
}
if(total>0){
if(window.confirm('共選中'+total+'首歌,是否繼續(xù)?')){
window.open('about:blank','SubWin','');
return true;
}
else{
return false;
}
}
else{
window.alert('沒有選擇!') ;
return false;
}
}
</script>
<form target="SubWin" onsubmit="return check();">
<input type="checkbox" name="oBox">歌曲一
<input type="checkbox" name="oBox">歌曲二
<input type="checkbox" name="oBox">歌曲三
<input type="button" value="看看">
</form>
您可能感興趣的文章:
- jquery判斷checkbox(復(fù)選框)是否被選中的代碼
- 動(dòng)態(tài)獲取復(fù)選框checkbox選中個(gè)數(shù)的jquery代碼
- Jquery獲取復(fù)選框被選中值的簡(jiǎn)單方法
- jquery獲取復(fù)選框被選中的值
- jquery統(tǒng)計(jì)用戶選中的復(fù)選框的個(gè)數(shù)
- jQuery實(shí)現(xiàn)統(tǒng)計(jì)復(fù)選框選中數(shù)量
- JQuery限制復(fù)選框checkbox可選中個(gè)數(shù)的方法
- JQuery判斷checkbox是否選中及其它復(fù)選框操作方法合集
- jQuery切換所有復(fù)選框選中狀態(tài)的方法
- javascript判斷復(fù)選框是否選中的方法
相關(guān)文章
jquery實(shí)現(xiàn)表格中點(diǎn)擊相應(yīng)行變色功能效果【實(shí)例代碼】
下面小編就為大家?guī)硪黄猨query實(shí)現(xiàn)表格中點(diǎn)擊相應(yīng)行變色功能效果【實(shí)例代碼】。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考2016-05-05jQuery實(shí)現(xiàn)B2B網(wǎng)站后臺(tái)管理系統(tǒng)側(cè)導(dǎo)航
這篇文章主要介紹了jQuery實(shí)現(xiàn)B2B網(wǎng)站后臺(tái)管理系統(tǒng)側(cè)導(dǎo)航,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07jquery獲取點(diǎn)擊控件的絕對(duì)位置簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄猨query獲取點(diǎn)擊控件的絕對(duì)位置簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10jQuery實(shí)現(xiàn)磁力圖片跟隨效果完整示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)磁力圖片跟隨效果,結(jié)合完整實(shí)例形式分析了jQuery事件響應(yīng)及animate方法實(shí)現(xiàn)帶緩沖效果的圖片跟隨效果,需要的朋友可以參考下2016-09-09jQuery實(shí)現(xiàn)的Div窗口震動(dòng)效果實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的Div窗口震動(dòng)效果,可實(shí)現(xiàn)點(diǎn)擊提交后窗口出現(xiàn)震動(dòng)效果,需要的朋友可以參考下2015-08-08jquery帶翻頁(yè)動(dòng)畫的電子雜志代碼分享
jquery帶翻頁(yè)動(dòng)畫的電子雜志是一款jquery.booklet電子書小冊(cè)子帶翻頁(yè)動(dòng)畫的電子雜志書本,感興趣的小伙伴們可以參考一下2015-08-08