This article collects a large number of conclusions about the select radio box for jquery operations. For more information, see.
Select Box Operation
Get value
The Code is as follows: |
Copy code |
$ ('# Select'). val () |
Select the item whose value is xx
The Code is as follows: |
Copy code |
$ ('# Select'). val (xx) |
Set the text to yy to selected
The Code is as follows: |
Copy code |
$ ('# Select option [text = "yy"]'). attr ("selected", true ); |
Obtain the index selected by the select statement:
The Code is as follows: |
Copy code |
$ ("# DdlRegType"). get (0). selectedIndex; |
Set select:
Set the index selected by select:
The Code is as follows: |
Copy code |
$ ("# DdlRegType"). get (0). selectedIndex = index; // index indicates the index value. |
Set the value selected by select:
The Code is as follows: |
Copy code |
$ ("# DdlRegType"). attr ("value", "Normal"); $ ("# DdlRegType"). val ("Normal "); $ ("# DdlRegType"). get (0). value = value; |
Set select selected text:
The Code is as follows: |
Copy 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; } } $ ("# Select_id option [text = 'jquery ']"). attr ("selected", true ); |
Set select option items:
The Code is as follows: |
Copy 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 |
Radio box operations
Get value
The Code is as follows: |
Copy code |
$ ('Input [name = "xx"]: checked'). val () |
Select if the value is xx.
The Code is as follows: |
Copy code |
$ ('Input [name = "xx"]: radio [value = "yy"] '). attr ('checked', 'checked '); |
Obtains the value of a set of radio selected items.
The Code is as follows: |
Copy code |
Var item = $ ('input [@ name = items] [@ checked] '). val (); $ ("Input [@ type = radio] [@ checked]"). val (); |
2. Set the. radio Group
1. $ ('input [@ name = items] '). get (1). checked = true; // The second element is the currently selected value.
2. $ ("input [@ type = radio] [@ value = 2]"). attr ("checked", true); // set the item with value = 2 to the selected item
The following describes some common instances.
HTML code in the drop-down box
The Code is as follows: |
Copy code |
<SELECT id = select> <OPTION selected> Chinese province </OPTION> </SELECT> |
Note that the select id here is "select"
Add "Jiangsu" to the last digit in the drop-down list
Javascript code
The Code is as follows: |
Copy code |
$ ('# Add_to_last '). click (function () {$ ('# select '). append ('<OPTION value = "Jiangsu"> </OPTION> Jiangsu ');});
|
Add "Anhui" to the first place in the drop-down list
Javascript code
The Code is as follows: |
Copy code |
$ ('# Add_to_first '). click (function () {$ ('# select '). prepend ('<OPTION value = "Anhui"> </OPTION> Anhui ');}); |
Obtain the current selectIndex (index of the project in the selected drop-down menu)
Javascript code
The Code is as follows: |
Copy code |
$ ('# Get_select_index'). click (function () {alert ($ ('# select option: selected'). attr ("index "));});
|
Remove the last project from the drop-down menu
Javascript code
The Code is as follows: |
Copy code |
$ ('# Remove_last_option'). click (function () {$ ('# select option: last'). remove ();}); |
Remove all options except the first one
The Code is as follows: |
Copy code |
$ ('# Remove_all_option_effect_first'). click (function () {$ ('# select option'). not (': first'). remove ();}); |
Obtain the maximum index value from the drop-down menu
The Code is as follows: |
Copy code |
$ ('# Get_max_index '). click (function () {var maxIndex =$ ("# select option: last "). attr ("index"); alert (maxIndex );}); |