Trying to get tag-it to work with an AJAX call

2019-01-10 03:48发布

Trying to get tag-it to work with an ajax call.

Everything works so far. Except, I am unable to assign a tagSource via an ajax call.

In firebug, the 'data' is returning:

["Ruby","Ruby On Rails"]

But its not showing up as I type into the input box.

$('.tags ul').tagit({
  itemName: 'question',
  fieldName: 'tags',
  removeConfirmation: true,
  availableTags: ["c++", "java", "php", "javascript", "ruby", "python", "c"],
  allowSpaces: true,
  // tagSource: ['foo', 'bar']
  tagSource: function() {
    $.ajax({
      url:        "/autocomplete_tags.json",
      dataType:   "json",
      data:       { term: 'ruby' },
      success:    function(data) {
        console.log(data);
        return data;
      }
    });
  }
});

console.log(data) returns ["Ruby", "Ruby On Rails"].

Am I missing something here? Anyone else got this to work?

7条回答
淡お忘
2楼-- · 2019-01-10 04:32

You should remove your availableTags, as you are overloading tagSource, which is used as source for the autocompletion.

Also that may be a typo, but it "return", and not "eturn".

Edit:

I think the problem is that the function you provided to tagSource doesn't seems to return anything. However my javascript knowledge seems to end here :/

查看更多
登录 后发表回答