LoginSignup
1
1

More than 5 years have passed since last update.

selectboxにoption要素の追加

Last updated at Posted at 2016-01-28

ポップアップから入力した値を親画面のselectにoptionとして追加する部分で
はまったので備忘録も兼ねてメモ

例)value=1 text=あ のoptionを追加する

chromeでは以下のように記述すればうまくいったけど
IEで確認すると親画面にappendしてくれなかった・・・

test_chrome.js
window.opener.$('[name=selectbox]').append(function(){
        //値が重複しているならば追加しない
        if(window.opener.$('[name=selectbox] option[value = 1]').size() == 0) {
            return $("<option>").val("1").text("");
        }

調べてみたらIEで要素をappendしようとするのがよくないらしく
以下のように修正したらうまく動きました。
(jQuery素人なので意味なくfunction使っていたのでそこも切り取り)

test_ie.js
if(window.opener.$('[name=selectbox] option[value = 1]').size() == 0) {
        var a = '<option value ="1">あ</option>'
        window.opener.$('[name=selectbox]').append(a);
    };
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1