JS獲取checkbox的個數(shù)簡單實例
更新時間:2016年08月19日 09:59:38 投稿:jingxian
下面小編就為大家?guī)硪黄狫S獲取checkbox的個數(shù)簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
JS獲取多選框checkbox被選中的個數(shù)。
var checkbox = document.getElementsByName("likes[]"); //此處通過此種方式才能獲得多選框為數(shù)組。 //like為name = "like[]" , 獲得時必須加上[] var checked_counts = 0; for(var i=0;i<checkbox.length;i++){ if(checkbox[i].checked){ //被選中的checkbox checked_counts++; } } alert(checked_counts);
我做的是每點擊一下多選框就判斷當前checked個數(shù)是否超過某個數(shù)值
function checkDate(){ var n = $("#cart_q_num").val(); var checkedCount=0; var checkbox = document.getElementsByName("tie_in[]"); //alert(checkbox.length); for(var i=0;i<checkbox.length ;i ++){ if(checkbox[i].checked){ checkedCount++; } } //alert(checkedCount); if(checkedCount>n){ alert("The quantity of the gifts should equal to the quantity of the sunglasses set."); return false; }else{ $("#free_pro_selected_num").html(checkedCount); } }
要使函數(shù)checkdata()每次點擊都發(fā)揮作用,需要在checkbox框中添加onclick事件:
<input type="checkbox" name="tie_in[]" value="1" onClick="return checkData()" />
以上這篇JS獲取checkbox的個數(shù)簡單實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- JS遍歷ul下的li點擊彈出li的索引的實現(xiàn)方法
- Jquery和JS獲取ul中l(wèi)i標簽的實現(xiàn)方法
- javascript操作ul中l(wèi)i的方法
- JS動態(tài)增加刪除UL節(jié)點LI及相關(guān)內(nèi)容示例
- js 動態(tài)添加元素(div、li、img等)及設置屬性的方法
- js 獲取元素下面所有l(wèi)i的兩種方法
- Javascript 獲取LI里的內(nèi)容
- js獲取對象、數(shù)組的實際長度,元素實際個數(shù)的實現(xiàn)代碼
- js獲取下拉列表的值和元素個數(shù)示例
- JavaScript獲取多個數(shù)組的交集簡單實例
- js限制checkbox勾選的個數(shù)以及php獲取多個checkbbox的方法深入解析
- JavaScript獲取ul中l(wèi)i個數(shù)的方法