【问题标题】:Uncaught TypeError: $(...).tagsinput is not a function未捕获的类型错误:$(...).tagsinput 不是函数
【发布时间】:2018-05-26 13:44:20
【问题描述】:

我有一个搜索框和几个复选框。选中后,复选框中的值将被推送到搜索框中。

现在我想对推入搜索框中的每个字符串使用 Bootstrap 标签输入,使其看起来像一个标签,但我在标题中收到错误消息。

我使用标签输入错误吗?

$(document).ready(function () {
        $checks = $(":checkbox");
        $checks.on('change', function () {
            var ingredient = $checks.filter(":checked").map(function (i, v) {
                return this.id;
            }).get().join(" ");
            $('#SearchString').tagsinput(ingredient);
        }).trigger('change');
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<link href="https://cdn.jsdelivr.net/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.css" rel="stylesheet" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://code.jquery.com/jquery-3.2.1.js"></script>
    <script src="https://cdn.jsdelivr.net/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.min.js"></script>
    
    
   <input class="form-control" id="SearchString" data-role="tagsinput" name="SearchString" type="text" value="">

【问题讨论】:

  • 可能是第二个jquery.js链接造成的,不会吧?没有理由链接jquery两次,第二次在taginput.js之后

标签: jquery checkbox input twitter-bootstrap-3 tags


【解决方案1】:

这是你修改后的 sn-p:

$(document).ready(function () {
      $checks = $(":checkbox");
      $checks.on('change', function () {
          $('#SearchString').tagsinput('removeAll');
          $checks.filter(":checked").each(function( index ) {
              $('#SearchString').tagsinput('add', this.value);
          });
     }).trigger('change');
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<link href="https://cdn.jsdelivr.net/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.css" rel="stylesheet" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://code.jquery.com/jquery-3.2.1.js"></script>
    <script src="https://cdn.jsdelivr.net/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.min.js"></script>
    
    
   <input class="form-control" id="SearchString" data-role="tagsinput" name="SearchString" type="text" value="">

<div class="checkbox">
  <label><input id="chk1" type="checkbox" value="ingredient1">ingredient 1</label>
</div>
<div class="checkbox">
  <label><input id="chk2" type="checkbox" value="ingredient2">ingredient 2</label>
</div>
<div class="checkbox disabled">
  <label><input id="chk3" type="checkbox" value="ingredient3">ingredient 3</label>
</div>

使用对象而不是纯文本作为标签的改进版本可在此处获得:https://jsfiddle.net/beaver71/v44mnn31/16/

在此示例中,当删除 tagsinput 中的标签时,相关复选框中将取消选中它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 2019-06-06
    • 2019-05-24
    • 2021-12-15
    • 2019-10-26
    • 2016-06-27
    相关资源
    最近更新 更多