We usually do projects, often encounter the need to realize the full selection of checkbox, reverse selection and assignment, online there are many examples, here to share is my common method, recommended to everyone.
Copy Code code as follows:
Whether the JS value is in the array
Array.prototype.in_array = function (e) {
for (i=0;i<this.length;i++) {
if (this[i] = = e)
return true;
}
return false;
}
JS Array 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 ');//select all increase 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");//anti-select Delete IDs
Houseids=[];
alert (houseids);
}
})
Single-Select Increase 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);
}
}
That's all the code for this example, and I want to help you learn how to use JavaScript to control a checkbox.