A simple example of JS getting select value and text value

Source: Internet
Author: User

A simple example of JS getting select value and text value

This article mainly introduces a simple example of JS getting select value and text value. If you need a friend, you can refer to it and hope to help you.

The Code is as follows:

<Select id = "cityList">

<Select id = "selectId">

<Option value = "0"> 0th </option>

</Select>

 

<Script>

Var selectObj = document. getElementById ('selectid ');

// Add option through object

SelectId. add (new Option ("first", "1 "))

SelectId. add (new Option ("second", "2 "))

// Add option by id

SelectId. add (new Option ("third", "3 "))

SelectId. add (new Option ("fourth", "4 "))

// Add method by id (you can also add method by object)

SelectId. onchange = function (){

// Obtain the value and text through the object

Alert (selectObj. value );

Alert (selectObj. options [selectObj. selectedIndex]. text );

// Obtain value and text by id

Alert (selectId. value );

Alert (selectId. options [selectId. selectedIndex]. text );

// You can also use this to obtain the value and text

Alert (this. value );

Alert (this. options [this. selectedIndex]. text );

};

</Script>

 

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.