JQuery's methods for obtaining and setting Select Options are summarized as follows:
Code:
Copy codeThe Code is as follows:
$ ("# Select_id"). change (function () {// code...}); // adds an event for Select, which is triggered when one of the selected events is selected
Var checkText = $ ("# select_id"). find ("option: selected"). text (); // obtain the selected Text
Var checkValue = $ ("# select_id"). val (); // obtain the Value selected by Select
Var checkIndex = $ ("# select_id"). get (0). selectedIndex; // obtain the index value selected by Select
Var maxIndex = $ ("# select_id option: last"). attr ("index"); // obtain the largest index value of Select
$ ("# Select_id"). get (0). selectedIndex = 1; // Select an item whose Select index value is 1
$ ("# Select_id"). val (4); // Select an item whose Value is set to 4.
$ ("# Select_id option [text = 'jquery ']"). attr ("selected", true); // Select an item whose Select Text value is set to jQuery
Obtain the selected text in the select statement:
Copy codeThe Code is as follows:
$ ("# DdlRegType"). find ("option: selected"). text ();
Obtain the value selected by the select statement:
Copy codeThe Code is as follows:
$ ("# Nowamagic"). val ();
Obtain the index selected by the select statement:
Copy codeThe Code is as follows:
$ ("# Nowamagic"). get (0). selectedIndex;
Set select
JQuery adds/deletes Select Option items:
Copy codeThe Code is as follows:
$ ("# Select_id"). append ("<option value = 'value'> Text </option>"); // append an Option to Select (drop-down)
$ ("# Select_id"). prepend ("<option value = '0'> Select </option>"); // insert an Option for Select (first position)
$ ("# Select_id option: last"). remove (); // Delete the largest Option (last) index value in Select)
$ ("# Select_id option [index = '0']"). remove (); // Delete the Option with the index value 0 in Select (the first option)
$ ("# Select_id option [value = '3']"). remove (); // Delete the Option whose Value is '3' in Select.
$ ("# Select_id option [text = '4']"). remove (); // Delete the Option of '4' Text = '4' in Select.
Set the index selected by select:
Copy codeThe Code is as follows:
// Index is the index value
$ ("# Nowamagic"). get (0). selectedIndex = index;
Set the value selected by select:
Copy codeThe Code is as follows:
$ ("# Nowamagic"). attr ("value", "Normal ");
$ ("# Nowamagic"). val ("Normal ");
$ ("# Nowamagic"). get (0). value = value;
Set select selected text:
Copy codeThe Code is as follows:
Var count = $ ("# nowamagicoption"). length;
For (var I = 0; I <count; I ++)
{If ($ ("# nowamagic"). get (0). options [I]. text = text)
{
$ ("# Nowamagic"). get (0). options [I]. selected = true;
Break;
}
}
Clear select:
Copy codeThe Code is as follows:
$ ("# Nowamagic"). empty ();