A simple all-choice Jquery plug-in

Source: Internet
Author: User
In actual development, the full-selection operation is often used. Here, a Jquery full-selection plug-in is written to facilitate the direct call of [javascript]/** jQuerylightweightpluginCheckAll * Originalauthor: Suifengshiqu * Furtherchanges, comm...

In actual development, all-selection operations are often used. Here, a Jquery all-selection plug-in is written to facilitate future direct calls.
[Javascript]
/*
* JQuery lightweight plugin CheckAll
* Original author: Suifengshiqu
* Further changes, comments: lvbo1988@gmail.com
*/
 
/* Select all INS in the table */
 
/* Usage */
/*
$ ("# Tab"). CheckAll ({valueField: "txtTest", chkId: "chkall", splitExp: "|", valueIndex: 0 });
*/
 
(Function ($ ){
$. Fn. extend ({
CheckAll: function (options ){
Var defaults = {
ValueField: null, // The default value field selected
ChkId: "c_all", // Id of the all-selected box
SplitExp: "_", // delimiter of the field
ValueIndex: 1 // index subscript of the split Value
};
Var options = $. extend (defaults, options );
Obj = $ (this); // The default value is table.
Function _ getSelectedValue (){
Var values = "";
$ ("#" + Obj. attr ("Id") + ": checked"). each (function (){
If ($ (this). attr ("id ")! = Options. chkId ){
Values + = $ (this). attr ("id"). split (options. splitExp) [options. valueIndex] + ",";
}
});
If (values. length> 0)
Return values. substring (0, values. length-1 );
Return values;
}
Return this. each (function (){
Var subExp = "#" + obj. attr ("Id") + ": checkbox ";
Var chks = $ (subExp + ": gt (0 )");
Var checkedCount = 0;
Chks. each (function (){
$ (This). click (function (){
If ($ (this). attr ("checked ")){
CheckedCount + = 1;
} Else {
CheckedCount-= 1;
}
If (checkedCount <chks. length ){
$ ("#" + Options. chkId). attr ("checked", false );
} Else {
$ ("#" + Options. chkId). attr ("checked", true );
}
$ ("#" + Options. valueField). val (_ getSelectedValue ());
});
});
$ ("#" + Options. chkId). click (function (){
$ (SubExp). attr ("checked", $ (this). attr ("checked ")? True: false );
If ($ (this). attr ("checked ")){
$ (SubExp). attr ("checked", true );
CheckedCount = $ (subExp + ": gt (0)"). length;
} Else {
$ (SubExp). attr ("checked", false );
CheckedCount = 0;
}
$ ("#" + Options. valueField). val (_ getSelectedValue ());
});
});
}
});
}) (JQuery );

 

The page call is as follows:
[Html]
 
 
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.