Jquery gets the value of checkboxlist
Checkboxlist is a server control. It is easy to bind data and the server can obtain the selected value. However, the generated static page does not have the value of listitem. Therefore, by default, the value of listitem cannot be obtained on the page using Js.
Problem description: JS cannot obtain the value of the selected listitem.
<Asp: checkboxlist
Runat = "server"
Id = "listtest">
</ASP: checkboxlist>
<Input
Type = "button"
Id = "btnshow"
Value = "show selected values"
/>
The generated static html is as follows:
<Table id = "listtest"
Border = "0">
<Tr>
<TD>
<Input
Id = "listtest_0"
Type = "checkbox"
Name = "listtest $0"
/>
<Label
For = "listtest_0"> jquery-based vibration effect </label>
</TD>
</Tr>
<Tr>
<TD> <Input
Id = "listtest_1"
Type = "checkbox"
Name = "listtest $1"
/>
<Label
For = "listtest_1"> use the overflow attribute of CSS to change the thumbnail size </label>
</TD>
</Tr>
</Table>
Solution:
When you bind a checkboxlist object, add an ALT attribute for each object in the listitem object and save the corresponding value. The Code is as follows:
If (DT! = NULL & DT. Rows. Count>
0)
{
Foreach (datarow Dr
In DT. Rows)
{
// Text and value respectively
Listtest. Items. Add (New listitem (Dr ["title"]. tostring (), Dr ["ID"]. tostring ()));
}
// Add the alt attribute to the listitem object and save the value
Foreach (listitem Li
In listtest. Items)
{
Li. Attributes. Add ("Alt", Li. value );
}
}
// Type 2
Protected void chkbind ()
{
String sel = "select product_id, productname from product ";
Sqldatareader DR = sqlhelper. executereader (sqlhelper. Conn, commandtype. Text, Sel, null );
Chkproduct. datatextfield = "productname ";
Chkproduct. datavaluefield = "product_id ";
Chkproduct. datasource = Dr;
Chkproduct. databind ();
Conn. Close ();
Foreach (listitem Li in chkproduct. Items)
{
Li. Attributes. Add ("Alt", Li. value );
}
}
<Table
Id = "Table1"
Border = "0">
<Tr>
<TD>
<Span
Alt = "400" type = "codeph" text = "codeph"> <Input
Id = "listtest_0"
Type = "checkbox"
Name = "listtest $0"
/>
<Label
For = "listtest_0"> jquery-based vibration effect </label> </span>
</TD>
</Tr>
<Tr>
<TD> <Span
Alt = "398" type = "codeph" text = "codeph"> <Input
Id = "listtest_1"
Type = "checkbox"
Name = "listtest $1"
/>
<Label for = "listtest_1"> use the overflow attribute of CSS to change the thumbnail size </label> </span>
</TD>
</Tr>
</Table>
The following figure shows how JS retrieves the value of the selected listitem:
$ (Document). Ready (function (){
$ ("# Btnshow"). Click (function (){
VaR ValueList =
"";
// Save the checkbox selected value
// Traverse the checkbox whose name starts with listtest
$ ("Input [name ^ = 'listtest']"). Each (function ()
{
If (this. Checked ){
// $ (This): The current checkbox object;
// $ (This). Parent ("span"): checkbox parent span object
ValueList + = $ (this). Parent ("span"). ATTR ("Alt ")
+ ",";
}
});
If (ValueList. length>
0 ){
// Obtain the sequence of the selected checkbox values. The result is 400,398.
ValueList = ValueList. substring (0, ValueList. Length-
1 );
}
});
});