开发者

Get the selected option from a list

开发者 https://www.devze.com 2022-12-12 07:01 出处:网络
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!!

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消