The Text/value value of the jquery control select is selected _jquery

Source: Internet
Author: User
Every time you select the operation, always in the online turned down, too cumbersome, the summary of their own here.

Like <select class= "selector" ></select>

1, set the value of "all" item selected
Copy Code code as follows:

$ (". Selector"). Val ("All");

2, set the text to "all" item selected
Copy Code code as follows:

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

Here is a use of brackets, preceded by an equal sign in brackets, with the attribute name, without quotes. In many cases, the use of brackets can make logic simple.

3, get the value of the currently selected item
Copy Code code as follows:

$ (". Selector"). Val ();

4, get the text of the currently selected item
Copy Code code as follows:

$ (". Selector"). Find ("option:selected"). Text ();

Using a colon, mastering its usage and extrapolate it makes the code concise.

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

Such as:
Copy Code code as follows:

$ (". Selector1"). Change (function () {

Empty the second one first.

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

In practical applications, the option here is generally to generate multiple loops

var option = $ ("<option>"). Val (1). Text ("Pxx");

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

});

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.