我需要一个允许自由文本的多行文本框,但如果我开始输入字符:“@@”
应显示带有可用标签的自动完成框,并允许我从现有标签中选择标签,标签名称中不应出现“@@”字符。
目前我正在使用tag-it plugin播放jquery UI,但无法弄清楚如何允许自由文本,只能在“@@”上输入自动完成框。以及如何使用textarea而不是常规输入。
另外,我想强迫他们从列表中选择是否输入@@并且不允许输入自由文本(第一个可用的选择会很好)
Javascript:
$(document).ready(function() {
//The demo tag array
var availableTags = [
{value: 1, label: 'tag1'},
{value: 2, label: 'tag2'},
{value: 3, label: 'tag3'}];
//The text input
var input = $("input#text");
//The tagit list
var instance = $("<ul class=\"tags\"></ul>");
//Store the current tags
//Note: the tags here can be split by any of the trigger keys
// as tagit will split on the trigger keys anything passed
var currentTags = input.val();
//Hide the input and append tagit to the dom
input.hide().after(instance);
//Initialize tagit
instance.tagit({
tagSource:availableTags,
tagsChanged:function () {
//Get the tags
var tags = instance.tagit('tags');
var tagString = [];
//Pull out only value
for (var i in tags){
tagString.push(tags[i].value);
}
//Put the tags into the input, joint by a ','
input.val(tagString.join(','));
}
});
//Add pre-loaded tags to tagit
instance.tagit('add', currentTags);
});
html:
<p>This example shows how to use Tagit on an input!</p>
<input type="text" id="text" name="tags" value="one,two,three"/>
答案 0 :(得分:4)
因为你已经使用了tag-it插件..我已经在输入中添加了一些处理程序来处理
@@
在您键入时显示自动完成@@
我仍然需要时间来查看 如果键入@@
,请不要允许自由文本DEMO: http://jsfiddle.net/xBgfJ/2/及以下是完整代码,
注意:以下代码调整到现有的插件代码。
$(document).ready(function() {
//The demo tag array
var availableTags = [{value: 1, label: 'tag1'},{ value: 2,label: 'tag2'}, { value: 3, label: 'tag3'}];
//The text input
var input = $("input#text");
//The tagit list
var instance = $("<ul class=\"tags\"></ul>");
//Store the current tags
//Note: the tags here can be split by any of the trigger keys
// as tagit will split on the trigger keys anything passed
var currentTags = input.val();
//Hide the input and append tagit to the dom
input.hide().after(instance);
//Initialize tagit
instance.tagit({
tagSource: availableTags,
tagsChanged: function() {
//Get the tags
var tags = instance.tagit('tags');
var tagString = [];
//Pull out only value
for (var i in tags) {
tagString.push(tags[i].value);
}
//Put the tags into the input, joint by a ','
input.val(tagString.join(','));
},
onTagAdded: function() {
inpNext.parent().find('.pre-filter').remove();
}
});
//Add pre-loaded tags to tagit
instance.tagit('add', currentTags);
var inpNext = input.next();
var autoCompelteMenu = $('.ui-autocomplete', inpNext);
inpNext.on('keydown', '.tagit-input', function(e) {
var $parent = $(this).parent();
var $preFilter = $parent.find('.pre-filter');
if (e.which == 8 && this.value == '') { //backspace
$preFilter.remove();
} else if (e.which == 9 || e.which == 32
|| e.which == 188 || e.which == 44 ||
e.which == 13 ) { //tab or space, comma and enter
$preFilter.remove();
autoCompelteMenu.css('opacity', 0);
}
}).on('keypress', '.tagit-input', function(e) {
var $parent = $(this).parent();
var $preFilter = $parent.find('.pre-filter');
if (e.which == 64 && !$preFilter.length) {
$parent.prepend('<span class="pre-filter hidden">@</span>');
autoCompelteMenu.css('opacity', 0);
} else if ( e.which == 64 && $preFilter.length) {
e.preventDefault();
this.value = '';
$preFilter.append('@').removeClass('hidden');
autoCompelteMenu.css('opacity', 1);
}
return;
}).on('blur', '.tagit-input', function() {
$(this).parent().find('span').remove();
autoCompelteMenu.css('opacity', 0);
});
});
答案 1 :(得分:0)
我为此创建了一个Meteor包,它允许自由文本和多个自动完成源。 Meteor的数据模型允许使用自定义渲染列表进行快速多规则搜索。如果你不使用Meteor作为你的网络应用程序,(我相信)你很遗憾找不到任何真正自动完成的东西。
使用@
自动填充用户,其中在线用户以绿色显示:
在同一行中,使用!
使用元数据和引导程序图标自动填充其他内容:
分叉,拉动和改进: