0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【メモ】jQueryで動的なselectボックスにSelect2を適用する

Posted at

背景

こちらの記事を参考にselect2を適用したselectボックスを複製しようとしましたが
複製元のselect2の適用が消えたり不具合が多発しました

解決法

コピー元からselect2を削除し、削除した行をコピー
挿入後それぞれに適用

app.js
    //コピーアイコン押した際の行コピー
    $('.js-copy-row').on('click', function (e) {
        let row = $(this).closest("tr");
        // select2の破棄
        row.find(".select2").select2('destroy');
        // 新規追加行
        let newRow = row.clone(true);
        newRow.insertAfter(row);
        row.find(".select2").select2();
        newRow.find(".select2").select2();
   })

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?