jquery Select-related operations

Source: Internet
Author: User

Cases:

<class= "selector"  ID= "selector"></  Select>

1. Set value to Pxx item selected

$ ("#selector"). Val ("Pxx");

2. Set text to Pxx to select

Set the selection according to text
$ ("#selector"). Find ("option[text= ' Pxx ')"). attr ("selected",true);
Based on value to set the selected
$ ("#selector"). Find ("option[value= ' Pxx ')"). attr ("selected",true);

Here is the use of a bracket, preceded by the attribute name in parentheses, without quotation marks. In many cases, the use of brackets can make logic very simple.

3. Gets the value of the currently selected item

Notice that you want to use the IE8

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

$ ("#selector"). Val ();

4. Gets the text of the currently selected item

Notice that you want to use the IE8

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

$ ("#selector"). Find ("option:selected"). Text ();

Using a colon here, mastering its usage and extrapolate will also make the code concise.

There are many times when a select cascade is used, that is, the value of the second select changes with the value selected in the first select. This is very simple in jquery.

Such as:

$ (". Selector1"). Change (function() {     ///  first empty the second       $ (". Selector2 "). empty ();      // in real-world applications, the option here is typically generated by looping multiple      var option = $ ("<option>"). Val (1). Text ("Pxx");      $ (". Selector2"). Append (option);});

jquery Select-related operations

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.