jquery統(tǒng)計復選框選中示例
更新時間:2013年11月05日 17:19:04 作者:
在jquery中如果我們要獲取選中的checkbox我們可以input:checked,然后獲取選中checkbox個數(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判斷復選框的選中個數(shù)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
window.onload = function(){//頁面所有元素加載完畢
var btn = document.getElementById("btn"); //獲取id為btn的元素(button)
btn.onclick = function(){ //給元素添加onclick事件
var arrays = new Array(); //創(chuàng)建一個數(shù)組對象
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( "選中的個數(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="你選中的個數(shù)" id="btn"/>
</form>
</body>
</html>
通過jQuery獲取checkbox選中項的個數(shù),需要用到j(luò)Query的size()方法或length屬性,下面的例子是通過length屬性獲得checkbox選中項的個數(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>
復制代碼 代碼如下:
<!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判斷復選框的選中個數(shù)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
window.onload = function(){//頁面所有元素加載完畢
var btn = document.getElementById("btn"); //獲取id為btn的元素(button)
btn.onclick = function(){ //給元素添加onclick事件
var arrays = new Array(); //創(chuàng)建一個數(shù)組對象
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( "選中的個數(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="你選中的個數(shù)" id="btn"/>
</form>
</body>
</html>
通過jQuery獲取checkbox選中項的個數(shù),需要用到j(luò)Query的size()方法或length屬性,下面的例子是通過length屬性獲得checkbox選中項的個數(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>
相關(guān)文章
jquery下將選擇的checkbox的id組成字符串的方法
jquery下將選擇的checkbox的id組成字符串的方法,需要的朋友可以參考下。2010-11-11
ff下JQuery無法監(jiān)聽input的keyup事件的解決方法
這篇文章主要介紹了ff下JQuery無法監(jiān)聽input的keyup事件的解決方法。需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12
jQuery實現(xiàn)炫麗的3d旋轉(zhuǎn)星空效果
這篇文章主要介紹了jQuery實現(xiàn)炫麗的3d旋轉(zhuǎn)星空效果,涉及jQuery數(shù)值運算與頁面元素屬性動態(tài)變換相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
jquery下為Event handler傳遞動態(tài)參數(shù)的代碼
在jquery cook book里看到一篇給event handler傳遞動態(tài)參數(shù)的文章 感覺挺實用的 跟大家分享下2011-01-01
jQuery ajax提交Form表單實例(附demo源碼)
這篇文章主要介紹了jQuery ajax提交Form表單的方法,結(jié)合實例分析了jQuery ajax操作實現(xiàn)表單提交的相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-04-04

