jquery 为什么select2不工作,当我附加html

wxclj1h5  于 6个月前  发布在  jQuery
关注(0)|答案(1)|浏览(90)

我在使用select2时遇到问题。我想更改类别过滤器,但当追加select2中的数据不起作用时。有什么解决方案可以解决这个问题?


的数据

$(document).on('change', '#filterbySelect1', function (event) {
    event.preventDefault();
    var selectVal = $('#filterbySelect1').val();
    if (selectVal == 'categoryBook') {
        $('.filter').append('<div class="form-group mb-5">'
            + '<label for="selectbyCategory">Filter Berdasarkan Katagori</label>'
            + '<select class="select2bs4" multiple="multiple" data-placeholder="Pilih Kategori" style="width: 100%;">'
            + '<option>Alabama</option>'
            + '<option>Alaska</option>'
            + '</select>'
            + '<small id="emailHelp" class="form-text text-muted">Maksimal memilih 3 kategori</small>'
            + '</div>');
    }
});

//Initialize Select2 Elements
$('.select2').select2();

//Initialize Select2 Elements
$('.select2bs4').select2({
    theme: 'bootstrap4',
    dropdownParent: $('.filter')
});

字符串

q1qsirdb

q1qsirdb1#

当你重新创建新的select时,在select2创建的情况下初始化:

$(document).on('change', '#filterbySelect1', function (event) {
    event.preventDefault();
    var selectVal = $('#filterbySelect1').val();
    if (selectVal == 'categoryBook') {
        $('.filter').append('<div class="form-group mb-5">'
            + '<label for="selectbyCategory">Filter Berdasarkan Katagori</label>'
            + '<select class="select2bs4" multiple="multiple" data-placeholder="Pilih Kategori" style="width: 100%;">'
            + '<option>Alabama</option>'
            + '<option>Alaska</option>'
            + '</select>'
            + '<small id="emailHelp" class="form-text text-muted">Maksimal memilih 3 kategori</small>'
            + '</div>');
           
           //Initialize Select2 Elements
           $('.select2bs4').select2({
              theme: 'bootstrap4',
              dropdownParent: $('.filter')
           });

    }
});

//Initialize Select2 Elements <--- initialize another select2?
$('.select2').select2();

//Initialize Select2 Elements
//$('.select2bs4').select2({
//    theme: 'bootstrap4',
//    dropdownParent: $('.filter')
//});

字符串

相关问题