Jquery obtains the value of the dynamically generated checkboxlist.

Source: Internet
Author: User

 

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 );
       }
   });
});

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.