【问题标题】:jquery select2 prepopulation not working: the field is emptyjquery select2 预填充不起作用:该字段为空
【发布时间】:2016-03-13 23:27:30
【问题描述】:

只是无法让 select2 预先填充标签字段。代码很简单

$.getJSON('http://pblog/tags/show/' + post, function(data) {
        console.log(data);
        $('#tags').select2({
          data: data,
          placeholder: 'choose your tags',
          tags: true,
        });
      });

网址返回:

[{"result":46,"text":"Food"},{"result":48,"text":"Travel"}]

但该字段仍为空。当我读到 initSelection 已被删除,所以我真的不知道缺少什么。

【问题讨论】:

    标签: jquery tags jquery-select2 populate


    【解决方案1】:

    我创建了一个小的 api 接口来检索所需的标签。 代码如下所示:

      $.getJSON('http://pblog/tags/show/' + post, function(data) {
            for (var d = 0; d < data.length; d++) {
              var item = data[d];
    
              // Create the DOM option that is pre-selected by default
              var option = new Option(item.text, item.text, true, true);
    
              // Append it to the select
              $('#tags').append(option);
            }
            $('#tags').trigger('change');
          });
    

    第一行的url是api地址,响应的是post标签的json数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2021-12-10
      • 2011-12-29
      • 1970-01-01
      • 2017-07-01
      • 2015-08-10
      • 2011-02-01
      相关资源
      最近更新 更多