javascript實(shí)現(xiàn)checkBox的全選,反選與賦值
我們平時(shí)在做項(xiàng)目的時(shí)候,經(jīng)常會(huì)遇到需要實(shí)現(xiàn)實(shí)現(xiàn)checkBox的全選,反選與賦值的情況,網(wǎng)上也有許多的范例,這里給大家分享的是本人常用的方法,推薦給大家。
//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);
}
}
以上就是本示例的全部代碼了,希望對(duì)大家學(xué)習(xí)使用javascript控制checkbox有所幫助。
- 利用Vue.js實(shí)現(xiàn)checkbox的全選反選效果
- js實(shí)現(xiàn)checkbox全選、不選與反選的方法
- 兩種不同的方法實(shí)現(xiàn)js對(duì)checkbox進(jìn)行全選和反選
- js實(shí)現(xiàn)checkbox全選和反選示例
- 實(shí)現(xiàn)checkbox全選、反選、取消JavaScript小腳本異常
- jquery、js操作checkbox全選反選
- js操作CheckBoxList實(shí)現(xiàn)全選/反選(在客服端完成)
- Jquery CheckBox全選方法代碼附j(luò)s checkbox全選反選代碼
- javaScript checkbox 全選/反選及批量刪除
- javascript checkbox全選和反選的簡(jiǎn)單實(shí)現(xiàn)
相關(guān)文章
js簡(jiǎn)單實(shí)現(xiàn)刪除記錄時(shí)的提示效果
刪除記錄時(shí)的提示效果,挺人性化的,實(shí)現(xiàn)的方法有很多,在本文為大家介紹下使用js是如何實(shí)現(xiàn)的2013-12-12

js實(shí)現(xiàn)Tab選項(xiàng)卡切換效果

非常好用的JsonToString 方法 簡(jiǎn)單實(shí)例

ECMAScript6函數(shù)剩余參數(shù)(Rest Parameters)