You need to click on an option in the Drop-down list, to save the contents of the selection, below to share with you how to use JS to get the Drop-down list box text value, the need for friends can refer to the next
A recent problem is that you need to save the selection by clicking on one of the options in the Drop-down list, such as the following HTML code:
Copy Code code as follows:
<select onchange= "isselected (this.value);" id= "City" >
<option value= "1" > Beijing </option>
<option value= "2" > Shanghai </option>
<option value= "2" > Guangzhou </option>
</select>
This means that when the user chooses the "Shanghai" column, the name "Shanghai" needs to be preserved. In fact, the method is very simple. Look at the following JavaScript code:
Copy Code code as follows:
function isselected (value) {
var CityName;
var city = document.getElementById ("city");
//Get the selected city name
for (i=0;i<city.length;i++) {
if (city[i].selected==true) {
cityname = City[i].innertext; Key Point
alert ("CityName:" + cityname);
}
}
You can also do this:
Copy Code code as follows:
function isselected (value) {
var city = document.getElementById ("city");
alert (city.options[city.selectedindex].innertext);
}
To roughly explain, first on the HTML page there is a Drop-down box, and for this reason the Drop-down framed a "city" id, and for it bound a onchange event, through this event call JavaScript function.
In JavaScript functions, the node element of the current Drop-down box is obtained through the Domcument object, because the value of the node is not only one, so we can come to the value of each option through the Loop node. At the time of the loop, by determining whether the current option is selected or not, if checked, use the City[i].innertext method to get the currently selected text value. Of course, if you need to get the option value, just do it: City[i].value.
At this point, through the above methods in IE has been able to achieve the desired results. However, when the Firefox test, found that this method does not work, and finally through the search data to find another method. Change the City[i].innertext to City[i].text. This method is applicable to IE and Fixefox!