带有强制和自由文本的自动填充标签

use*_*076 8 tags jquery jquery-ui

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

目前我正在使用jquery UI的tag-it插件,但无法弄清楚如何允许自由文本,只能在"@@"上输入自动完成框.以及如何使用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);
});
Run Code Online (Sandbox Code Playgroud)

HTML:

<p>This example shows how to use Tagit on an input!</p>
<input type="text" id="text" name="tags" value="one,two,three"/>
?
Run Code Online (Sandbox Code Playgroud)

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

Sel*_*gam 4

由于您使用了 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);
    });
});
Run Code Online (Sandbox Code Playgroud)