我正在尝试基于自动完成向 jQuery 插件标记添加一些功能:
a) 我尝试过滤我的 JSON 数据以仅显示标签名称。
由以下人员返回的 JSON 样本/repo/json
:
[{id:1, name:"0.8-alpha-1", category:"version"}, {id:2, name:"0.8-alpha-2", category:"version"}, {id:3, name:"0.8-alpha-3", category:"version"}, {id:4, name:"0.8-alpha-4", category:"version"}, {id:5, name:"0.8-alpha-1", category:"version"}, {id:6, name:"0.8-alpha-2", category:"version"}, {id:7, name:"0.8-alpha-3", category:"version"}, {id:8, name:"0.8-alpha-4", category:"version"}]
b)我想在用户提交数据时提交标签的 id,而不是名称。
c) 我尝试在我的 tag-it 输入字段中添加一些约束:用户无法验证不在我返回的 JSON 中的标签/repo/json call
。
我不想分叉 tag-it 存储库,似乎可以测试用户数组和使用beforeTagAdded
选项搜索之间的交集。
我这次尝试没有成功,因为我不知道在哪里可以找到标签列表来实现交集。
我的js代码:
$(function(){
$("#singleFieldTags").tagit({
tagSource: function(search, showChoices) {
$.ajax({
url: "/repo/json",
dataType: "json",
data: {q: search.term},
success: function(choices) {
showChoices(choices);
}
})},
beforeTagAdded: function(event, ui) {
//if ($.inArray(ui.tagLabel, search) == -1) {
// $("#singleFieldTags").tagit("removeTagByLabel", ui.tagLabel);
// }
console.log(ui.tag);
}});
});
html表单:
<form name="data" action="/repo/uploadMole" method="POST" enctype="multipart/form-data">
<input name="tags" id="singleFieldTags" ><br/>
<input type="Submit">
</form>