jQuery-tokeninput failing: “term” is undefined?

2019-05-10 17:39发布

问题:

In my rails application I am trying to use jquery-tokeninput. I have everything working - json response and all - but the script errors out when anything is typed into the tokenized field, complaining:

TypeError: term is undefined    
--> return term.replace(regexp_special_chars, '\\$&');

Both jQuery as well as the tokeninput scripts are loaded. What could cause this?

回答1:

It sounds like you might need to specify propertyToSearch. By default, Tokeninput looks for a "name" field, but if you don't have that field in the json response, you need to tell it what to be looking at. For example, if your json looks like this:

[
  {"id":"856","title":"House"},
  {"id":"1035","title":"Desperate Housewives"},
  ...
]

Then you need to make sure you include this:

$('#tvshow_tokens').tokenInput('/tvshows.json', { propertyToSearch: 'title' });