Get the selected option from a list
I'm trying to get the selected option via jQuery e开发者_如何转开发verytime I change it, and eventually the value attribute, buy I always have problems with js!!
HTML
<select>
<option value="us">United States</option>
<option value="gb">United Kingdom</option>
<option value="de" selected="selected">Germany</option>
<option value="fr">France</option>
</select>
jQuery
$("select").change (function () {
// what can I do there?
});
$('select').change(function() {
alert($(this).val());
});
You can use:
$("select").change (function () {
var selectedValue = $("select").val();
var selectedText = $("select:selected").text();
});
$("select").val()
will always give you the value of the selected option at any point.
精彩评论