JavaScript Gets the text value sample code in the Drop-down list box

Source: Internet
Author: User
Tags html page
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.