【问题标题】:Select2 get html data attributeselect2 获取html数据属性
【发布时间】:2017-03-17 04:50:18
【问题描述】:

我正在使用最新的 select2 版本。

但卡在获取<select> html 属性。

<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); 

我遵循了这个解决方案How to get data attribute of <select> for use in select2 from inside .select2() ajax call? 和这里https://github.com/select2/select2/issues/3361,但仍然没有运气。

我的data-ajax--search 总是return undefined

有什么办法吗?

【问题讨论】:

    标签: jquery jquery-select2


    【解决方案1】:

    尝试使用 $(this).data('ajax-Search') 而不是 $(this.element).data('search')

    【讨论】:

      猜你喜欢
      • 2019-12-23
      • 1970-01-01
      • 2015-07-27
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多