尝试让 tag-it 与 AJAX 调用一起使用
尝试让 tag-it 处理 ajax 调用。
到目前为止一切正常。除此之外,我无法通过 ajax 调用分配 tagSource。
在 firebug 中,“数据”正在返回:
["Ruby","Ruby On Rails"]
但当我在输入框中键入内容时,它没有显示。
$('.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)
返回["Ruby", "Ruby On Rails"]
。
我在这里错过了什么吗?还有其他人让这个工作吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
似乎这个问题已经很长时间没有得到解答了,我敢打赌您已经找到了解决方案,但对于那些还没有找到解决方案的人,这是我的答案:
当我从代码复制时,缩进变得一团糟;)
实际上,您可以在一些自动完成 jQuery 示例中找到“search.php”。
Seems this question hasn't been answered for a long time, I bet you have already found a solution but for those who haven't here is my answer:
The indention got all messed up when i copied from my code ;)
and the "search.php" you can find this in some autocomplete jQuery examples actually.
看看这个:
如何让 tagSource 与 $.ajax() 一起使用? (来自 tag-这是 github 问题列表)。
这是一个例子:
HTML 文件:(
从[此处][1]获取 tag-it.js 文件)
这是 PHP 文件:
check this out:
How to get tagSource to work with $.ajax()? (from tag-it's github issue list).
this is an example:
HTML file:
(get tag-it.js file from [here][1])
and this is the PHP file:
这些答案对我来说都不起作用,所以我想我会回来并发布我的有效代码。
此代码假设 URL 返回 JSON 编码的字符串(字符串数组)。然后,它将过滤掉输入中已选择的任何标签。所以它们不会在列表中重复。否则,这对我有用。
感谢其他人让我走上正确的道路。
None of these answers worked as is for me, so I thought I would come back and post my code that does work.
This code assumes the URL returns a JSON encoded string (an array of strings). It will then filter out any tags that have already been selected in the input. So they aren't repeated in the list. Otherwise, this works for me.
Thanks to the others for sending me on the right path.
tagSource 已折旧。
只需使用:
您可以将所有属性添加到此:
tagSource has been depreciated.
Just use:
You can add all the attributes to this:
您应该删除
availableTags
,因为您正在重载tagSource
,它用作自动完成的源。另外,这可能是一个拼写错误,但它是“
return
”,而不是“eturn
”。编辑:
我认为问题是您提供给
tagSource
的函数似乎没有返回任何内容。然而我的 javascript 知识似乎就到此为止了:/You should remove your
availableTags
, as you are overloadingtagSource
, 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 :/我认为您可以覆盖 jquery UI 中的自动完成方法:
I think that you can overwrite the autocomplete method from jquery UI :
只是添加
假设您的脚本页面看起来像
您的简单 php 页面,如果您从数据库中获取值,则看起来像
问候
Just to add
Assuming your script page looks like
Your simple php page if you are fetching values from database would look like
Regards