Home  >  Article  >  Web Front-end  >  Code summary for jquery operation select option_jquery

Code summary for jquery operation select option_jquery

WBOY
WBOYOriginal
2016-05-16 18:05:421100browse

1. Get the value and text of the selected selection. The html code is as follows:

Copy the code The code is as follows:



The selected value and text can be obtained through the following script codes
Copy code The code is as follows:

$("#mySelect").val(); //Get The value of the selected record
$("#mySelect option:selected").text(); //Get the text value of the selected record

2. Use new Option("text", "Value") method adds option option
Copy code The code is as follows:

var obj = document .getElementById("mySelect");
obj.add(new Option("4","4"));

3. Delete all options option
Copy code The code is as follows:

var obj = document.getElementById("mySelect");
obj.options. length = 0;

4. Delete the selected option option
Copy code The code is as follows:

var obj = document.getElementById("mySelect");
var index = obj.selectedIndex;
obj.options.remove(index);

5. Modify the selected option option
Copy code The code is as follows:

var obj = document.getElementById ("mySelect");
var index = obj.selectedIndex;
obj.options[index] = new Option("three",3); //Change the corresponding value
obj.options[index ].selected = true; //Keep selected

6. Delete select
Copy code The code is as follows:

var obj = document.getElementById("mySelect");
obj.parentNode.removeChild(obj); //Remove the current object

7. Select the selected response event
Copy code The code is as follows:

$("# mySelect").change(function(){
//Add the operation code that needs to be executed
})
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn