Jquery obtains the client control select

Source: Internet
Author: User

Http://ghl68754031.k15.wg8.com/

Jquery obtains the client control select

1. Obtain the value related to the selected text in the SELECT statement.

Obtain the selected text: var checktext = $ ("# slc1"). Find ("option: Selected"). Text ();

Obtain the value selected by select: var checkvalue = $ ("# slc1"). Val ();

Obtain the index value selected by select: var checkindex = $ ("# slc1"). Get (0). selectedindex;

Obtain the largest index value of select: var maxindex =$ ("# slc1 option: Last"). ATTR ("Index ");

Ii. set text and value selected by select

Select an item whose select index value is 1: $ ("# slc1"). Get (0). selectedindex = 1;

Select an item whose value is 4: $ ("# slc1"). Val (4 );

Set the select text value to jquery:

$ ("# Slc1 option [TEXT = 'jquery ']"). ATTR ("selected", true );

PS: pay special attention to the use of the third item. Look at jquery's selector feature so powerful!

3. add and delete option items

Append an option to select (drop-down)

$ ("# Slc2"). append ("<option value = '" + I + "'>" + I + "</option> ");

Insert an option for select (first position)

$ ("# Slc2"). prepend ("<option value = '0'> select </option> ");

PS: prepend this is the best way to insert content to the beginning of all matching elements.

Delete the maximum option (last one) of the index values in the SELECT statement)

$ ("# Slc2 option: Last"). Remove ();

Delete option (first) with index value 0 in select)

$ ("# Slc2 option [Index = '0']"). Remove ();

Delete option with value = '3' in select

$ ("# Slc2 option [value = '3']"). Remove ();

Delete the option of '4' text = '4' in the SELECT statement.

$ ("# Slc2 option [TEXT = '3']"). Remove ();

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.