简体   繁体   English

jQuery Select2数据属性

[英]jQuery Select2 data attributes

I implemented the search from the list of options via Ajax and is working perfectly. 我通过Ajax从选项列表中实现了搜索,并且工作正常。 However, to fill other cascading selects, I would save some html5 "data" attributes on populated options. 但是,要填充其他级联选项,我会在填充选项上保存一些html5“数据”属性。 The method "results" only expects an array with the ids and texts. 方法“结果”只需要一个带有ids和文本的数组。 Any idea? 任何的想法?

    results: function(data, page) {
      return { results: $.map(data, function(city, i) {
        return {id: city.id, text: city.name};
        // I tried adding data- attributes to return but didn't work
      })}
    }
  }

You can use formatResult . 您可以使用formatResult http://jsfiddle.net/LUsMb/4098/ http://jsfiddle.net/LUsMb/4098/

$("#e4").select2({
    formatResult: function(state) {
        return '<div data-my="' + ... + '">' + state.text + '</div>';
    },
    ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM