JavaScript > jQueryでselect要素を操作

更新日 2013-05-19
広告
jQueryでselect要素(id="select-id"とする)が持つoption要素をすべて取得するには、children関数を使います。
   var options = $('#select-id').children();
option要素の先頭を取得するには、さらにfirst関数を使います。
   var options = $('#select-id').children();
   var firstOptions = options.first();
選択されているoptionを取得するには、以下のようにします。
  var option = $('#select-id option:selected').val();
あるoptionを選択した状態にするには、そのoptionのvalueの値を指定します。
   $('#select-id').val("value1");
これで、value="value1"となっているoptionが選択された状態になります。
広告
お問い合わせは sweng.tips@gmail.com まで。
inserted by FC2 system