How does JS obtain the value of the current select element?

Source: Internet
Author: User

If the selected attribute is not specified for all option elements under the select element, the first option is selected by default.

You can use select. selectedIndex to obtain the index of the selected option element.

You can use select. options [select. selectedIndex] to obtain the selected option element.

The option element text3 can be used to obtain the value Attribute value of the option element through option. value, that is, value3. You can use option. text to obtain the text in the option element, that is, text3.

If the option element does not define the value attribute, option. value cannot be obtained in IE, but Safari, Opera, and FireFox can still be obtained through option. value, which is the same as option. text.

You can use option. attributes. value & option. attributes. value. specified to determine whether the option element defines the value attribute.

Therefore, the script for obtaining the current select element value is as follows:

Copy to ClipboardReference: [www.bkjia.com] var getSelectValue = funtion (select ){
Var idx = select. selectedIndex,
Option,
Value;
If (idx>-1 ){
Option = select. options [idx];
Value = option. attributes. value;
Return (value & value. specified )? Option. value: option. text );
}
Return null;
}

Original article: http://www.planabc.net/2010/03/27/how_to_get_select_element_value/

Related Article

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.