JQuery method for obtaining the value of checkboxlist _ jquery

Source: Internet
Author: User
Recently I started a project and used the server space checkboxlist. It is convenient to use. It is a little troublesome to get the value from js. google found the following method, if you are interested, you can refer to the CheckboxList as a server control. It is easy to bind data and to obtain the selected value on the server. However, the generated static page does not have the Value of ListItem, so by default, the Value of ListItem cannot be obtained in the page using js. I don't know why the value is not displayed. This article provides a method for getting the checkboxlist value using jQuery.

First look at the original page html code:

 

The following code binds checkboxlist:

If (dt! = Null & dt. rows. count> 0) {foreach (DataRow dr in dt. rows) {// The text value and value listTest respectively. items. add (new ListItem (dr ["Title"]. toString (), dr ["ID"]. toString ()));}}

The html code generated on the page is as follows:

 
 
A vibration effect based on jQuery
Use the overflow attribute of css to change the thumbnail size.

It can be seen that the checkboxlist is converted into a table with no value. The value in the label, that is, the text value. When you click it, you can also select checkbox, which improves the user experience.
Next we will go to our processing process. First, when we bind the checkboxlist, we will add an alt attribute for each object in ListItem to save the corresponding value. The Code is as follows:

If (dt! = Null & dt. rows. count> 0) {foreach (DataRow dr in dt. rows) {// The text value and value listTest respectively. 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 );}}

The generated html code is as follows:

 
 
A vibration effect based on jQuery
Use the overflow attribute of css to change the thumbnail size.

We can see from the above that there is an extra span tag, and the alt value in it is the value we need. Use the following jQuery code to obtain:

$ (Document ). ready (function () {$ ("# btnShow "). click (function () {var valuelist = ""; // Save the checkbox selected value // traverse the checkbox $ ("input [name ^ = 'listtest']") whose name starts with 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 );}});});

The above is the method for jQuery to get the value of checkboxlist. I don't know if you really understand it. I hope this article will help you.

Related Article

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.