Select2 get html data attribute

淺唱寂寞╮ 提交于 2019-12-24 06:01:26

问题


I'm using latest select2 version.

But stuck at getting <select> html attribute.

<select name="category" id="category-select"
    data-ajax--search="slug" class="select2 form-control m-b-2" multiple>
</select>

// SCRIPT 

const opts = {
    ajax: {
        url: "/menus/search",
        delay: 250,
        data: function (param) {
            return {q: param.term, page: param.page, category: $(this.element).data('search')};
        },
        processResults: ({data, total_count}, {page}) => {
            page = page || 1;

            return {
                results: data,
                pagination: {
                    more: (page * 30) < total_count
                }
            };
        },
        cache: true
    },
    scapeMarkup: markup => markup,
    templateResult: formatRepo,
    templateSelection: formatRepoSelection
};
$('.select2').select2(opts); 

I followed this solution How to get data attribute of <select> for use in select2 from inside .select2() ajax call? and here https://github.com/select2/select2/issues/3361 but still no luck.

my data-ajax--search always return undefined.

Any solution?


回答1:


Try using $(this).data('ajax-Search') instead of $(this.element).data('search')



来源:https://stackoverflow.com/questions/42849440/select2-get-html-data-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!