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