Select statement for jquery operations
This article mainly introduces the select (set and select) Operation of jquery. If you need a friend, you can refer to it and hope to help you.
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:
The Code is as follows:
$ (". 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 );
});