【问题标题】:tagsinput-typeahead value as tagtagsinput-typeahead 值作为标签
【发布时间】:2016-12-08 00:04:34
【问题描述】:

我正在使用 Bootstrap 3.3.6、JQuery 1.11.0、Bootstrap3-typeahead 4.0.2 和 bootstrap-tagsinput 0.8.0。

我的 tagsinput 和 typeahead 工作正常,但是如何让标签显示值而不是从 typeahead 中选择的文本?

值 = 项目编号

文本 = 项目编号 + 项目名称

这是我的代码:

    $('#jobno').tagsinput({
          confirmKeys: [13],
          itemValue: 'value',
          itemText: 'text',
          typeahead: {
               displayKey: 'text',
               afterSelect: function(val) { this.$element.val(""); },
               minLength:3,
               source: [{"value":"2006.01","text":"2006.01 - Project 1"},{"value":"2006.02","text":"2006.02 - Project 2"},{"value":"2006.03","text":"2006.03 - Project 3"}]
          }
    });

<div class="form-group">
    <label class="control-label col-md-3">Job Number(s) </label>
    <div class="col-md-8">
        <!-- <input type="text" class="form-control tagsinput-typeahead" name="jobno" id="jobno"  > -->
        <select multiple="multiple" class="form-control tagsinput-typeahead" name="jobno" id="jobno" ></select>
    </div>
</div>

这些选项都不是我想要的:

itemValue: 'value',
itemText: 'value',

itemValue: 'text',
itemText: 'text',

还有第三种选择吗?

Screen Shot

【问题讨论】:

    标签: jquery twitter-bootstrap bootstrap-typeahead bootstrap-tags-input


    【解决方案1】:

    只需打开您的 bootstrap-tagsinput.js 并在以下位置更改您的自定义值:

      // add a tag element
    
      var $tag = $('<span class="tag ' + htmlEncode(tagClass) + (itemTitle !== null ? ('" title="' + itemTitle) : '') + '">'+htmlEncode(itemValue) +": " + htmlEncode(itemText) + '<span data-role="remove"></span></span>');
      $tag.data('item', item);
      self.findInputWrapper().before($tag);
      $tag.after(' ');
    

    '+htmlEncode(itemValue) +": " + htmlEncode(itemText) + ' 是在标签上显示文本的部分,在这种情况下它显示:“value: text”。

    【讨论】:

    • 我遇到了类似的问题。有什么办法不修改 bootstrap-tagsinput.js 文件..?
    猜你喜欢
    • 1970-01-01
    • 2015-06-04
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    相关资源
    最近更新 更多