Today, I encountered a problem when a netizen asked the Forum about all and invert selection of the input checkbox. I summarized it and wrote a simple jQuery checkbox invert selection plug-in. I hope it will be useful to everyone.
The following function can be used for simple selection and inverse selection of checkbox:
Function checkall (obj, cName)
{
Var checkboxs = document. getElementsByName (cName );
For (var I = checkboxs. length; I --;){
Checkboxs [I]. checked = obj. checked;
}
}
// Usage: add an onclick event to the selected button: onclick = "checkall (this, 'all [] ');"
The following jQuery checkbox plug-in is found online:
$. Fn. checkbox = function (){
Var t = this;
/*
* Switch all/reselect
* @ Example $ ("# checkAll"). checkbox (). toggle ($ ("input [name = 'selectall']");
*/
This. toggle = function (el ){
$ (El). click (function (){
$ (T). attr ('checked', false );
});
$ (This). click (function (){
$ (El). attr ('checked', $ (this). attr ('checked') = true? True: false );
});
};
/*
* Select all
*/
This. check = function (el ){
$ (El). attr ('checked', true );
};
/*
* Invert Selection
*/
This. uncheck = function (el ){
$ (El). attr ('checked', false );
};
Return t;
};