This article summarizes the implementation code of common operations on the select drop-down box in jquery and javascript. If you need to know it, you can refer to it.
Js select and option operations
1. dynamically create select
| The Code is as follows: |
Copy code |
|
Function createSelect (){
Var mySelect = document. createElement_x ("select ");
MySelect. id = "mySelect ";
Document. body. appendChild (mySelect );
} |
2. Add option
| The Code is as follows: |
Copy code |
|
Function addOption (){
// Search for objects by id,
Var obj = document. getElementByIdx_x ('myselect ');
// Add an option
Obj. add (new Option ("text", "value"); // This can only be valid in IE
Obj. options. add (new Option ("text", "value"); // compatible with IE and firefox
} |
3. Delete All option options
| The Code is as follows: |
Copy code |
|
Function removeAll (){
Var obj = document. getElementByIdx_x ('myselect ');
Obj. options. length = 0;
} |
4. delete an option
| The Code is as follows: |
Copy code |
|
Function removeOne (){
Var obj = document. getElementByIdx_x ('myselect ');
// Index. The sequence number of the option to be deleted. The sequence number of the selected option is used here.
Var index = obj. selectedIndex;
Obj. options. remove (index );
} |
5. Obtain the option value.
| The Code is as follows: |
Copy code |
|
Var obj = document. getElementByIdx_x ('myselect ');
Var index = obj. selectedIndex; // the serial number of the selected option.
Var val = obj. options [index]. value; |
6. Obtain the option text
| The Code is as follows: |
Copy code |
|
Var obj = document. getElementByIdx_x ('myselect ');
Var index = obj. selectedIndex; // the serial number of the selected option.
Var val = obj. options [index]. text; |
7. Modify option
| The Code is as follows: |
Copy code |
|
Var obj = document. getElementByIdx_x ('myselect ');
Var index = obj. selectedIndex; // the serial number of the selected option.
Var val = obj. options [index] = new Option ("new text", "new Value "); |
8. Delete select
| The Code is as follows: |
Copy code |
|
Function removeSelect (){
Var mySelect = document. getElementByIdx_x ("mySelect ");
MySelect. parentNode. removeChild (mySelect );
} |
Next, let's send the jquery + select/"target =" _ blank "> jquery select operation.
Syntax explanation:
| The Code is as follows: |
Copy code |
|
1. $ ("# select_id"). change (function () {// code...}); // Add an event for Select, triggered when one of the Select events is selected
2. var checkText = $ ("# select_id"). find ("option: selected"). text (); // obtain the selected Text
3. var checkValue = $ ("# select_id"). val (); // obtain the Value selected by Select
4. var checkIndex = $ ("# select_id"). get (0). selectedIndex; // obtain the index value selected by Select
5. var maxIndex = $ ("# select_id option: last"). attr ("index"); // obtain the largest index value of Select. |
JQuery sets the Text and Value selected by Select:
Syntax explanation:
| The Code is as follows: |
Copy code |
1. $ ("# select_id"). get (0). selectedIndex = 1; // Select an item whose Select index value is 1
2. $ ("# select_id"). val (4); // Select an item whose Value is set to 4.
3. $ ("# select_id option [text = 'jquery ']"). attr ("selected", true); // set the Select Text value to jQuery. |
JQuery adds/deletes Select Option items:
Syntax explanation:
| The Code is as follows: |
Copy code |
1. $ ("# select_id"). append ("Text"); // append an Option for Select (drop-down)
2. $ ("# select_id"). prepend ("Select"); // insert an Option for Select (first position)
3. $ ("# select_id option: last"). remove (); // Delete the largest Option (last) index value in Select)
4. $ ("# select_id option [index = '0']"). remove (); // Delete the Option with the index value 0 in Select (the first option)
5. $ ("# select_id option [value = '3']"). remove (); // Delete the Option with Value = '3' In the Select statement
5. $ ("# select_id option [text = '4']"). remove (); // Delete the Option of '4' Text = '4' in Select
|