JS to get the value and text of the selected items in the drop-down box

Source: Internet
Author: User

JSObtains the value and text of the selected item in the drop-down list.

JS provides two methods to get the value of the drop-down box and text online: But some people are very irresponsible, and they did not take into account the errors caused by the differences between browsers, as a result, many new users cannot find errors for a long time!

Below I will summarize the values and text of the selected items in the drop-down box in Firefox and IE:

1. Methods supported by IE and Firefox:

Get Text

VaROBJ = Document. getelementbyid ('select _ template ');

VaR text = obj. Options [obj. selectedindex]. Text; // get text

 

VaROBJ = Document. getelementbyid ("select_template ");

For(I = 0; I <obj. length; I ++) {// The length of the drop-down box is the number of options

If(OBJ [I]. Selected =True){

VaR text = OBJ [I]. Text; // get text

}

}

Compared with the previous method

 

 

2. ie does not support Firefox:

VaR OBJ = Document. getelementbyid (name );

For (I = 0; I <obj. length; I ++ ){
If (OBJ [I]. Selected = true ){
VaR text = OBJ [I]. innertext;
}
}

Methods for getting values IE and Firefox:

VaR value = Document. getelementbyid ("select_template"). value; // get the value

 

Conclusion: Both IE and Firefox support the value and text attributes. Firefox does not support the innertext attribute.

 

JS implements the current page to open a new link:

Window. Location. href = URL;

 

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.