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>