Use jquery to operate the code of the select drop-down list box

Source: Internet
Author: User

Example:
Copy codeThe Code is as follows:
<Select id = "sltList" name = "list">
<Option value = "1"> JOHN </option>
<Option value = "2"> Li Si </option>
</Select>
// Obtain the selected option value
$ ('# SltList'). val ()
// Obtain the text of the selected item
$ ('# SltList option [@ selected]'). text (); // obtain the currently selected option. text is text, and val is the option value.
Or var item = $ ("select [@ name = list] option [@ selected]"). text ();
// Set the text to the selected item
$ ("# SltList option [@ selected]"). attr ("text", 'zhang san ');

// Set the second element of the select drop-down box to the selected value.
$ ('# SltList') [0]. selectedIndex = 1;
// Set the project with value = 1 as the currently selected item
$ ("# SltList"). attr ("value", '1 ');
$ ('# SltList'). val ('1 ');
// Add the option in the drop-down list
$ ("<Option value = '3'> Wang Wu </option> <option value = '4'> Zhao Liu </option>"). appendTo ("# sltList ")
// Clear the drop-down list
$ ("SltList"). empty ();
Example:
// Events during change
$ ("# TestSelect"). change (function () {// event generation
JQuery ('option: selected', this). each (function () {// print multiple values
Alert (this. value );
});
});
$ ("# ChildTypeResult"). append (htmlStr); // adds the value of htmlStr after this ID tag.
$ ("# ChildTypeResult" ).html (htmlStr); is the value of modifying this ID to htmlStr.

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.