A simple all-choice Jquery plug-in

Source: Internet
Author: User

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]
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
<Script src = "Scripts/jquery-1.7.1.min.js" type = "text/javascript"> </script>
<Script src = "Scripts/jquery. CheckAll. js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("# Tab"). CheckAll ({valueField: "txtTest "});
});
</Script>
</Head>
<Body>
<Table id = "tab">
<Tr>
<Td> <input type = "checkbox" id = "c_all"/> </td>
</Tr>
<Tr>
<Td> <input type = "checkbox" id = "c_1"/> </td>
</Tr>
<Tr>
<Td> <input type = "checkbox" id = "c_2"/> </td>
</Tr>
<Tr>
<Td> <input type = "checkbox" id = "c_3"/> </td>
</Tr>
<Tr>
<Td> <input type = "checkbox" id = "c_4"/> </td>
</Tr>
</Table>
<Input type = "text" id = "txtTest"/>
<Span onclick = "alert (txtTest. value);"> View selected items </span>
</Body>
</Html>


From the column of QQlvbo

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.