简体   繁体   English

jquery select2 预填充不起作用:该字段为空

[英]jquery select2 prepopulation not working: the field is empty

Just can't get the select2 to pre populate the tags field.只是无法让 select2 预先填充标签字段。 The code is quite simple代码很简单

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

The url returns this:该网址返回:

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

yet the field stays empty.然而该领域仍然是空的。 As I read the initSelection was removed, so I don't really know what is missing.当我读到 initSelection 被删除时,所以我真的不知道缺少什么。

I edded up creating a little api interface, to retrieve the needed tags.我最终创建了一个小 api 接口,以检索所需的标签。 The code for that looks like this:代码如下所示:

  $.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');
      });

The url at the first line is the api adress which respons is a json array of the post tags.第一行的 url 是 api 地址,它响应的是 post 标签的 json 数组。

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

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