javascript實現(xiàn)checkBox的全選,反選與賦值
更新時間:2015年03月12日 08:50:44 投稿:hebedich
這篇文章主要介紹了javascript實現(xiàn)checkBox的全選,反選與賦值的方法,以實例形式詳細分析了實現(xiàn)的思路及對應(yīng)的html與js代碼的實現(xiàn)過程
我們平時在做項目的時候,經(jīng)常會遇到需要實現(xiàn)實現(xiàn)checkBox的全選,反選與賦值的情況,網(wǎng)上也有許多的范例,這里給大家分享的是本人常用的方法,推薦給大家。
復(fù)制代碼 代碼如下:
//js 數(shù)值是否在數(shù)組中
Array.prototype.in_array = function(e){
for(i=0;i<this.length;i++){
if(this[i] == e)
return true;
}
return false;
}
//js數(shù)組index
Array.prototype.find_str=function(string){
var str = this.join("");
return str.indexOf(string);
}
var houseIds=new Array();
$("#chebox-list-all").click(function(){
if($("#chebox-list-all").attr("checked")){
$("[name='checkboxes']").attr("checked",'true');//全選 增加id
var ids = document.getElementsByName('checkboxes');
var value = new Array();
for(var i = 0; i < ids.length; i++){
if(ids[i].checked)
houseIds.push(ids[i].value);
}
alert(houseIds);
}else{
$("[name='checkboxes']").removeAttr("checked");//反選 刪除Ids
houseIds=[];
alert(houseIds);
}
})
//單選增加id
function check(obj){
if(!houseIds.in_array(obj.value)){
houseIds.push(obj.value);
alert(houseIds);
}else{
var index=houseIds.find_str(obj.value);
houseIds.splice(index, 1)
alert(houseIds);
}
}
以上就是本示例的全部代碼了,希望對大家學(xué)習(xí)使用javascript控制checkbox有所幫助。
您可能感興趣的文章:
- 利用Vue.js實現(xiàn)checkbox的全選反選效果
- js實現(xiàn)checkbox全選、不選與反選的方法
- 兩種不同的方法實現(xiàn)js對checkbox進行全選和反選
- js實現(xiàn)checkbox全選和反選示例
- 實現(xiàn)checkbox全選、反選、取消JavaScript小腳本異常
- jquery、js操作checkbox全選反選
- js操作CheckBoxList實現(xiàn)全選/反選(在客服端完成)
- Jquery CheckBox全選方法代碼附j(luò)s checkbox全選反選代碼
- javaScript checkbox 全選/反選及批量刪除
- javascript checkbox全選和反選的簡單實現(xiàn)
相關(guān)文章

ECMAScript6函數(shù)剩余參數(shù)(Rest Parameters)
這篇文章主要介紹了ECMAScript6函數(shù)剩余參數(shù)(Rest Parameters)的相關(guān)資料,需要的朋友可以參考下
2015-06-06