2014/07/18

[jQuery]selectboxのtext値を取得

selectboxで選択したoptionタグのtext値を取得するにはどうすればいいのだろうか?

下で試したらダメだった。

<script>
jQuery(function($){
  $('#hoge').on('change',function(e){
    console.log($(this).text())
  });
});
</script>
<select id="hoge">
  <option value="1">apple</option>
  <option value="2">orange</option>
  <option value="3">banana</option>
  <option value="4">lemon</option>
</select>
stackoverflowのGet selected text from drop down list (select box) using jQueryを読むと下でいけました。
<script>
jQuery(function($){
  $('#hoge').on('change',function(e){
    console.log($('#hoge option:selected').text())
  });
});
</script>
<select id="hoge">
  <option value="1">apple</option>
  <option value="2">orange</option>
  <option value="3">banana</option>
  <option value="4">lemon</option>
</select>

0 コメント:

コメントを投稿