bootstrap-tagsinput的使用简介

本文介绍了一个基于Bootstrap的标签输入插件——bootstrap-tagsinput,并演示了如何结合Typeahead实现标签建议功能,通过具体代码示例展示了如何初始化插件并加载预设标签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在做标签系统的时候,为了便于用户对标签的操作,如输入或是删除,用到了bootstrap-tagsinput。主要是用到了其中的Typeahead部分。
效果如下:
这里写图片描述
代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <script src="http://cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></script>
    <script src="http://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
    <script src="http://cdn.bootcss.com/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.min.js"></script>
    <script src="http://cdn.bootcss.com/typeahead.js/0.11.1/typeahead.bundle.js"></script>
    <link href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet">
    <link href="http://cdn.bootcss.com/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.css" rel="stylesheet">
    <style type="text/css">
    .icon-github {
        width: 16px;
        height: 16px;
    }

    .bootstrap-tagsinput {
        width: 100%;
    }

    .accordion {
        margin-bottom:-3px;
    }

    .accordion-group {
        border: none;
    }

    .twitter-typeahead .tt-query,
    .twitter-typeahead .tt-hint {
        margin-bottom: 0;
    }

    .twitter-typeahead .tt-hint
    {
        display: none;
    }

    .tt-menu {
        position: absolute;
        top: 100%;
        left: 0;
        z-index: 1000;
        display: none;
        float: left;
        min-width: 160px;
        padding: 5px 0;
        margin: 2px 0 0;
        list-style: none;
        font-size: 14px;
        background-color: #ffffff;
        border: 1px solid #cccccc;
        border: 1px solid rgba(0, 0, 0, 0.15);
        border-radius: 4px;
        -webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
        background-clip: padding-box;
        cursor: pointer;
    }

    .tt-suggestion {
        display: block;
        padding: 3px 20px;
        clear: both;
        font-weight: normal;
        line-height: 1.428571429;
        color: #333333;
        white-space: nowrap;
    }

    .tt-suggestion:hover,
    .tt-suggestion:focus {
      color: #ffffff;
      text-decoration: none;
      outline: 0;
      background-color: #428bca;
    }
    </style>
</head>
<body>
<input type="text" />
<script>
var cities = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: 'cities.json'  //标签所在文件,当然也可以从数据库中读取数据,然后生成json返回
});
cities.initialize();

var elt = $('input');
elt.tagsinput({
  itemValue: 'id',
  itemText: 'name',
  typeaheadjs: {
    name: 'cities',
    displayKey: 'name',
    source: cities.ttAdapter()
  }
});
elt.tagsinput('add', { "id": 0 , "name": "Washington"});
</script>
</body>
</html>

参考:https://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples

Bootstrap Tags Input :https://github.com/bootstrap-tagsinput/bootstrap-tagsinput
关于标签的插件还有很多,如
Amaze UI Tags Input :https://github.com/amazeui/tagsinput

### 关于 Bootstrap-tagsinput使用教程 #### 初始化与引入必要文件 为了最简单地使用 `bootstrap-tagsinput`,仅需引入五个必要的文件即可完成环境搭建[^1]。这些文件包含了所需的 JavaScript 和 CSS 资源。 ```html <!-- 引入CSS --> <link rel="stylesheet" href="path/to/bootstrap.min.css"> <link rel="stylesheet" href="path/to/bootstrap-tagsinput.css"> <!-- 引入JS --> <script src="path/to/jquery.min.js"></script> <script src="path/to/bootstrap.min.js"></script> <script src="path/to/bootstrap-tagsinput.min.js"></script> ``` 对于基于 Bootstrap 4 版本的插件,则可以访问特定项目页面获取最新资源链接[^2]。 #### 基础用法实例 通过简单的 HTML 输入框标签并应用 `.tagsinput()` 方法来初始化组件: ```html <input type="text" value="Amsterdam,Washington,Sydney,Beijing,Cairo" data-role="tagsinput"/> ``` ```javascript // 对指定的选择器执行初始化操作 $('input').tagsinput(); ``` 此代码片段会将输入字段转换成支持多选标签的形式,并自动解析初始值中的逗号分隔项作为默认标签显示。 #### 获取当前标签列表 当需要读取已有的标签数据时,可以通过如下方式实现: ```javascript var items = $("input").tagsinput('items'); console.log(items); // 输出所有标签组成的数组 ``` 这允许开发者轻松处理表单提交前的数据收集工作[^3]。 #### 进阶功能——分类标签 除了基本的功能外,还提供了更高级别的特性如对不同类型的标签进行分类展示等扩展能力。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值