gpt4 book ai didi

jquery - 带有强制和自由文本的自动完成标签

转载 作者:行者123 更新时间:2023-12-03 22:33:30 25 4
gpt4 key购买 nike

我需要一个允许自由文本的多行文本框,但如果我开始输入字符:“@@”
带有可用标签的自动完成框应该显示,并允许我从现有标签中选择标签,并且“@@”字符不应出现在标签名称中。

目前我正在玩 tag-it plugin对于jquery UI,但无法弄清楚如何允许自由文本并且仅在“@@”输入上显示自动完成框。以及如何使用文本区域而不是常规输入。

另外,如果他们输入 @@ 并且不允许输入自由文本,我想强制他们从列表中进行选择(第一个可用的选择会很好)
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"/>

在这里测试:http://jsfiddle.net/hailwood/u8zj5/

最佳答案

既然你使用了 tag-it 插件..我已经在输入中添加了一些处理程序来处理

  1. @@ 在您键入时显示自动完成
  2. 如果输入时不带 @@,则为自由文本

我仍然需要时间来研究如果输入@@则不允许自由文本

演示: 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);
});
});

关于jquery - 带有强制和自由文本的自动完成标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13390567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com