Select2-Ajax搜索-记住最近的结果

Select2-Ajax搜索-记住最近的结果,第1张

Select2-Ajax搜索-记住最近的结果

我确实再次阅读了您的帖子。我上次误会了你。

解决方案在这里。

   $(document).ready(function () {        $('#select').select2({ // this part is responsible for data caching dataCache: [], query: function (q) {     var obj = this,  key = q.term,  dataCache = obj.dataCache[key];     //checking is result in cache     if (dataCache) {         q.callback({results: dataCache.results});     } else {         $.ajax({  url: 'ajax.php',  data: {q: q.term},  dataType: 'json',  type: 'POST',  success: function (data) {      //copy data to 'cache'      obj.dataCache[key] = data;      q.callback({results: data.results});  }         })     } }, placeholder: 'Search something', width: '333', minimumInputLength: 3,        });        // this part is responsible for setting last search when select2 is opening        var last_search = '';        $('#select').on('select2-open', function () { if (last_search) {     $('.select2-search').find('input').val(last_search).trigger('paste'); }        });        $('#select').on('select2-loaded', function () { last_search = $('.select2-search').find('input').val();        });    });


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5560372.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-14
下一篇2022-12-14

发表评论

登录后才能评论

评论列表(0条)

    保存