HTML code:
Copy the code code as follows:
<select id="month" onchange="selectInput(this)">
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08" selected="selected">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
Use jQuery to get the value and text of the selected object in select
Copy the code code as follows:
$(function(){
$("#month").change(function() {
alert($(this).find("option:selected").val());
alert($(this).find("option:selected").text());
});
});
It is very easy to get the value of the selected object using JS.
Copy the code code as follows:
function selectInput(oSelect) {
alert(oSelect.value);
}
What if you want to use JS to get the text of the selected object?
Copy the code code as follows:
function selectInput(oSelect) {
alert(oSelect.options[oSelect.selectedIndex].text);
}
If there is no default in the select, set the first one as the default?
Copy the code code as follows:
document.getElementById("test").options[0].selected=true;