Jquery operation select (value, set selected), jqueryselect

Source: Internet
Author: User

Jquery operation select (value, set selected), jqueryselect

During each select operation, we always need to provide the materials. We recommend that you summarize the information and review it later.

For example, <select class = "selector"> </select>

1. Set the value to pxx.

$ (". Selector"). val ("pxx ");

2. Set text to pxx.

$ (". Selector"). find ("option [text = 'pxx']"). attr ("selected", true );

Here is the use of brackets. The equal signs in brackets are preceded by attribute names without quotation marks. In many cases, the use of brackets can simplify the logic.

3. Obtain the value of the selected item.

$ (". Selector"). val ();

4. Get the text of the selected item

$ (". Selector"). find ("option: selected"). text ();

The colon is used here to understand its usage and simplify the code.

 

The cascade of select is often used, that is, the value of the second select changes with the value selected by the first select. This is very simple in jquery.

For example: $ (". selector1"). change (function (){

// Clear the second one first

$ (". Selector2"). empty ();

// In practical applications, the options here are generally generated using multiple loops.

Var option = $ ("<option>"). val (1). text ("pxx ");

$ (". Selector2"). append (option );

});

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.