具有自动完成功能的 Tokenizer 插件。我实施得当吗?
Tokenizer plugin with autocomplete. Do I implement it properly?
我正在使用此处找到的插件 - http://www.jqueryscript.net/form/Simple-jQuery-Tagging-Tokenizer-Input-with-Autocomplete-Tokens.html
github - https://github.com/firstandthird/tokens
家属 - https://github.com/jgallen23/fidel
示例使用:
1) 工作示例(源数组加载到内存中)- http://jsfiddle.net/george_black/brmbyL8x/
js代码:
(function () {
$('#tokens-example').tokens({
source: ['Acura', 'Audi', 'BMW', 'Cadillac',
'Chrysler', 'Dodge', 'Ferrari', 'Ford',
'GMC', 'Honda', 'Hyundai', 'Infiniti',
'Jeep', 'Kia', 'Lexus', 'Mini',
'Nissan', 'Porsche', 'Subaru', 'Toyota',
'Volkswagon', 'Volvo'],
initValue: ['Acura', 'Nissan']
});
})();
html代码:
<h2>Cars</h2>
<input type="text" id="tokens-example" />
并且它工作正常。
2) 错误的配置示例? (源数组是从服务器生成并加载 ajax,实际上不是,但你明白了)- http://jsfiddle.net/george_black/xamxryn6/
function testFunctionFeed(query){
//dynamic results from server
return ['Acura', 'Audi', 'BMW', 'Cadillac',
'Chrysler', 'Dodge', 'Ferrari', 'Ford',
'GMC', 'Honda', 'Hyundai', 'Infiniti',
'Jeep', 'Kia', 'Lexus', 'Mini',
'Nissan', 'Porsche', 'Subaru', 'Toyota',
'Volkswagon', 'Volvo'];
}
(function () {
$('#tokens-example').tokens({
initValue: ['Acura', 'Nissan'],
query: function(query,callback){
//show query
$("#loggerQuery p").text(query);
//let's say the results are dynamic from server
var sgstions = testFunctionFeed(query);
console.log(sgstions);
$("#loggerCallback p").text(sgstions);
callback(sgstions);
}
});
})();
但是我在控制台中收到以下错误:
Uncaught TypeError: Cannot read property 'suggestions-list-element' of undefined
在令牌的 github 页面中,您可以阅读我在第二个示例中使用的配置。
"query": A function that is used to retreive suggestions. By default, it will use the internal sources,
however you can write your own function to query a database and return an array of suggestions.
This function receives two parameters
query : The value entered by the user
callback : The function that you should call, passing the suggestions as an array, once you finished getting your results
我的问题是,我在第二个示例中是否正确使用了插件?还有其他人使用过特定的分词器插件吗?
令牌具有我想要的标记器插件的确切行为(自定义标签、自动完成功能、简单的主题等),我真的很想在 ajax 调用中使用它。
我从一位插件开发人员那里得到了答复。
在我不工作的示例中,我在范围外调用回调函数(请记住,我没有开始的示例)。下面是正确的代码。
工作示例 - http://jsfiddle.net/george_black/vx8dnggh/
(function () {
$('#tokens-example').tokens({
initValue: ['Acura', 'Nissan'],
query: function(query,callback){
//show query
$("#loggerQuery p").text(query);
//let's say the results are dynamic from server
var sgstions = testFunctionFeed(query);
callback.call(this, sgstions);
}
});
})();
我正在使用此处找到的插件 - http://www.jqueryscript.net/form/Simple-jQuery-Tagging-Tokenizer-Input-with-Autocomplete-Tokens.html
github - https://github.com/firstandthird/tokens 家属 - https://github.com/jgallen23/fidel
示例使用: 1) 工作示例(源数组加载到内存中)- http://jsfiddle.net/george_black/brmbyL8x/
js代码:
(function () {
$('#tokens-example').tokens({
source: ['Acura', 'Audi', 'BMW', 'Cadillac',
'Chrysler', 'Dodge', 'Ferrari', 'Ford',
'GMC', 'Honda', 'Hyundai', 'Infiniti',
'Jeep', 'Kia', 'Lexus', 'Mini',
'Nissan', 'Porsche', 'Subaru', 'Toyota',
'Volkswagon', 'Volvo'],
initValue: ['Acura', 'Nissan']
});
})();
html代码:
<h2>Cars</h2>
<input type="text" id="tokens-example" />
并且它工作正常。
2) 错误的配置示例? (源数组是从服务器生成并加载 ajax,实际上不是,但你明白了)- http://jsfiddle.net/george_black/xamxryn6/
function testFunctionFeed(query){
//dynamic results from server
return ['Acura', 'Audi', 'BMW', 'Cadillac',
'Chrysler', 'Dodge', 'Ferrari', 'Ford',
'GMC', 'Honda', 'Hyundai', 'Infiniti',
'Jeep', 'Kia', 'Lexus', 'Mini',
'Nissan', 'Porsche', 'Subaru', 'Toyota',
'Volkswagon', 'Volvo'];
}
(function () {
$('#tokens-example').tokens({
initValue: ['Acura', 'Nissan'],
query: function(query,callback){
//show query
$("#loggerQuery p").text(query);
//let's say the results are dynamic from server
var sgstions = testFunctionFeed(query);
console.log(sgstions);
$("#loggerCallback p").text(sgstions);
callback(sgstions);
}
});
})();
但是我在控制台中收到以下错误:
Uncaught TypeError: Cannot read property 'suggestions-list-element' of undefined
在令牌的 github 页面中,您可以阅读我在第二个示例中使用的配置。
"query": A function that is used to retreive suggestions. By default, it will use the internal sources,
however you can write your own function to query a database and return an array of suggestions.
This function receives two parameters
query : The value entered by the user
callback : The function that you should call, passing the suggestions as an array, once you finished getting your results
我的问题是,我在第二个示例中是否正确使用了插件?还有其他人使用过特定的分词器插件吗?
令牌具有我想要的标记器插件的确切行为(自定义标签、自动完成功能、简单的主题等),我真的很想在 ajax 调用中使用它。
我从一位插件开发人员那里得到了答复。
在我不工作的示例中,我在范围外调用回调函数(请记住,我没有开始的示例)。下面是正确的代码。
工作示例 - http://jsfiddle.net/george_black/vx8dnggh/
(function () {
$('#tokens-example').tokens({
initValue: ['Acura', 'Nissan'],
query: function(query,callback){
//show query
$("#loggerQuery p").text(query);
//let's say the results are dynamic from server
var sgstions = testFunctionFeed(query);
callback.call(this, sgstions);
}
});
})();