我使用的是https://github.com/aehlke/tag-it/downloads的tag-it插件。如何禁止添加新标签?
$(document).ready(function () {
$("#Tags").tagit({
singleField: true,
singleFieldNode: $('#mySingleField'),
// onlyAvailableTags : true,
allowNewTags: false,
tagSource: [@Html.Raw(ViewBag.AvailableTags)]
});
});我尝试使用onlyAvailableTags : true和allowNewTags: false选项,但没有任何效果。
发布于 2012-04-03 22:51:38
既然你说“但是没有效果”,我猜@Html.Raw(ViewBag.AvailableTags)会产生一个违反javascript语法的输出。标签需要用引号括起来,否则它们将被视为变量。
错误输出:
tagSource: [my-tag, another-tag]在服务器端,我假设您有某种IEnumerable<string>
ViewBag.AvailableTags = new List<string>
{
"my-tag",
"another-tag",
};然后,在.cshtml中
tagSource: ["@string.Join("\", \"", ViewBag.AvailableTags)"]这将产生正确的输出:
tagSource: ["my-tag", "another-tag"]这将是我首先要尝试的。
发布于 2013-06-28 21:04:25
我通过注释发现了这一点:
// Autocomplete will create its own tag from a selection and close automatically.
if (!that.tagInput.data('autocomplete-open')) {
that.createTag(that._cleanedInput());
}和:
// Create a tag when the element loses focus.
// If autocomplete is enabled and suggestion was clicked, don't add it.
if (!that.tagInput.data('autocomplete-open')) {
that.createTag(that._cleanedInput());
}它删除了此功能。也许不是最干净的方式,但它是有效的。
只需注释掉if(){ }循环即可。
发布于 2015-05-28 09:17:03
这是我为最新版本的tag-it所做的:
var tags_list = ['tag1', 'tag2, 'tag3];
$("input[name='subject-tags']").tagit({
availableTags : tags_list,
beforeTagAdded : function(event, ui) {
if(tags_list.indexOf(ui.tagLabel) == -1){
return false;
}
}
});尝试通过实现以下方法使其更简洁
$("input[name='subject-tags']").tagit({
availableTags : ['tag1', 'tag2, 'tag3],
beforeTagAdded : function(event, ui) {
if(this.availableTags.indexOf(ui.tagLabel) == -1){
return false;
}
}
});但是this.availableTags不返回数组(返回:undefined)。我是一个JS新手,所以我访问属性的方式一定有问题。
https://stackoverflow.com/questions/8396369
复制相似问题