jQuery tags input 使用

来源:互联网 发布:淘宝靠谱的男装外贸店 编辑:程序博客网 时间:2024/06/05 19:51

由于项目中想使用tagsinput添加数据,找了很多资源,最终在github上找到开源的项目:

http://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples/

项目中详细介绍了tagsinput的使用,下面只讲解下添加数据的使用。

1、对于只是添加一个text,就很简单。直接在javascript中使用


$('input').tagsinput('add', 'some tag');
2、如果是想添加对象,那么就稍微复杂一点:

首先在<input>标签中不能加 data-role='tagsinput'属性。

然后在在javascript代码中:

var organization = new Bloodhound(
{
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('text'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: 'Resources/xenon/assets/organization.json'//json文件可以是空的,应该只是为了能够保存
});
organization.initialize();//初始化
$('#field-organizationresult').tagsinput({
itemValue: 'id',
itemText: 'text',
typeaheadjs: {
name: 'organization',
displayKey: 'text',//tagsinput显示的列
source: organization.ttAdapter()
  }
});
$('#field-organizationresult').tagsinput('add', {'id':json.id,'text':json.name});//在tagsinput中添加一个对象

Amsterdam Washington Sydney Beijing 

$('input').tagsinput('add', 'some tag');

Amsterdam Washington Sydney Beijing 
0 0
原创粉丝点击