【问题标题】:jquery select2 plugin, loading in default with ajax selectionjquery select2插件,默认加载ajax选择
【发布时间】:2013-09-06 21:46:34
【问题描述】:

我正在使用 jquery select 2 插件。 http://ivaynberg.github.io/select2/#ajax

我的搜索工作正常,我能够提取结果并选择正确的内容。但是当我重新加载页面时,我希望填充先前的选择。我有 id 和想要显示的内容,但似乎方法是使用 initSelection 调用。

这似乎有点矫枉过正,因为我已经有了要显示的文本......

$(".user-search").select2({
    minimumInputLength: 2,
    ajax: {
        url: "/users/search",
        dataType: 'json',
        data: function (term, page) {
            return { keyword: term };
        },
        results: function (data, page) {
            return { results: data };
        }
    }
});

<input name="user_id" class="user-search" type="text" style="width:100%" value="{{ $user->id }}" />

有什么方法可以做 $user->name 吗?也许在输入上将其设置为数据显示......

【问题讨论】:

    标签: jquery ajax jquery-select2


    【解决方案1】:

    我做了以下......

    ...code from the opening question,
    initSelection: function(element, callback) {
        var id=$(element).val();
        if (id!=="") {
          callback({id:id, text : $(element).attr('data-display')});
        }
    }
    

    完成了

    <input..... data-display="{{ $user->name }}">
    

    它有效,这是一个好方法吗?

    【讨论】:

      猜你喜欢
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 2015-11-15
      • 2013-04-15
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多