The methods used to trigger the select event include click () and change ()
Click (): triggered only when the object is clicked.
Change (): triggered when the object changes
Below is a simple record of jquery's common operations on select:
1. Obtain the selected text in the select statement:
Js Code
$ ("# Id"). find ("option: selected"). text ();
2. Obtain the selected value of select:
Js Code
$ ("# Id"). val ();
3. Obtain the selected index of the select statement:
Js Code
$ ("# Id"). get (0). selectedindex;
4. Set the index selected by the select statement:
Js Code
$ ("# Id"). get (0). selectedIndex = 1; // note that I uppercase
5. Set the value selected by select:
Js Code
$ ("# Id"). attr ("value", "normal");
$ ("# Id"). val ("normal ");
$ ("# Id"). get (0). value = value;
6. Set select selected text:
Js Code
Var count = $ ("# ddlregtype option"). length;
For (var I = 0; I <count; I ++)
{If ($ ("# ddlregtype"). get (0). options [I]. text = text)
{
$ ("# Ddlregtype"). get (0). options [I]. selected = true;
Break;
}
}
7. Set select option items:
Js Code
$ ("# Select_id"). append ("<option value = 'value'> text </option>"); // Add an option
$ ("# Select_id"). prepend ("<option value = '0'> select </option>"); // insert an option
$ ("# Select_id option: last"). remove (); // Delete the option with the largest index value
$ ("# Select_id option [index = '0']"). remove (); // delete an option with an index value of 0
$ ("# Select_id option [value = '3']"). remove (); // delete an option with a value of 3
$ ("# Select_id option [text = '4']"). remove (); // delete an option with a text value of 4
This article is from "Life is like a play"