jquery – 引导标签输入与类型头不工作

前端之家收集整理的这篇文章主要介绍了jquery – 引导标签输入与类型头不工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是参考 http://timschlechter.github.io/bootstrap-tagsinput/examples/bootstrap3/的“Bootstrap Tagsinput”

使用的插件:(最新版本)

引导3
> typeahead.js
> bootstrap-tagsinput.min.js

我想要的是使用Typeahead输入字段来添加标签.

  1. <div class="input-group col-sm-4">
  2. <input type="text" class="form-control" id="tagsinput" />
  3. </div>

jQuery部分在下面.

  1. $('#tagsinput').tagsinput({
  2. typeahead: {
  3. name: 'towns',local: ['Amsterdam','Washington','Sydney','Beijing','Cairo']
  4. }
  5. });

我分别尝试了文档页面和打字头文档页面.但没有找到任何解决方案.我实际上是测试这个简单的代码,所以我需要使用一个类似的事情的数据库.但即使它不适用于本地数据.

解决方法

这是一个引用3的标签输入的例子,它与typeahead.js一起工作:

图书馆:

> http://timschlechter.github.io/bootstrap-tagsinput/examples/bootstrap3/
> http://twitter.github.io/typeahead.js/
(显然,Bootstrap 3和jQuery)

需要注意的几件事情

>这是为多个taginput实例编写的(但是应该仍然适用于一个)
>未完成的输入在模糊中清晰
>任何无效的条目在添加时都被删除,并且启动了一个警报

HTML:

  1. <input type="text" class="stateinput" placeholder="Enter States" /><br />
  2. <input type="text" class="stateinput" placeholder="Enter States" /><br />
  3. <input type="text" class="stateinput" placeholder="Enter States" />

JavaScript的:

  1. $(function () {
  2.  
  3. // function from typeahead.js example
  4. var substringMatcher = function (strs) {
  5. return function findMatches(q,cb) {
  6. var matches,substringRegex;
  7.  
  8. // an array that will be populated with substring matches
  9. matches = [];
  10.  
  11. // regex used to determine if a string contains the substring `q`
  12. substrRegex = new RegExp(q,'i');
  13.  
  14. // iterate through the pool of strings and for any string that
  15. // contains the substring `q`,add it to the `matches` array
  16. $.each(strs,function (i,str) {
  17. if (substrRegex.test(str)) {
  18. // the typeahead jQuery plugin expects suggestions to a
  19. // JavaScript object,refer to typeahead docs for more info
  20. matches.push({ value: str });
  21. }
  22. });
  23.  
  24. cb(matches);
  25. };
  26. };
  27.  
  28. var states = ['Alabama','Alaska','Arizona','Arkansas','California','Colorado','Connecticut','Delaware','Florida','Georgia','Hawaii','Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts','Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada','New Hampshire','New Jersey','New Mexico','New York','North Carolina','North Dakota','Ohio','Oklahoma','Oregon','Pennsylvania','Rhode Island','South Carolina','South Dakota','Tennessee','Texas','Utah','Vermont','Virginia','West Virginia','Wisconsin','Wyoming'
  29. ];
  30.  
  31. var tags = $('input.stateinput');
  32.  
  33. // initialize tagsinput for each stateinput classed input
  34. tags.tagsinput();
  35.  
  36. $(tags).each(function (i,o) {
  37. // grab the input inside of tagsinput
  38. var taginput = $(o).tagsinput('input');
  39.  
  40. // ensure that a valid state is being entered
  41. $(o).on('itemAdded',function (event) {
  42. if (states.indexOf(event.item) < 0) {
  43. $(o).tagsinput('remove',event.item);
  44. alert(event.item + " is not a valid state");
  45. }
  46. });
  47.  
  48. // initialize typeahead for the tag input
  49. taginput.typeahead({
  50. hint: true,highlight: true,minLength: 1,autoselect: true
  51. },{
  52. name: 'states',displayKey: 'value',source: substringMatcher(states)
  53. }).bind('typeahead:selected',$.proxy(function (obj,datum) {
  54. // if the state is clicked,add it to tagsinput and clear input
  55. $(o).tagsinput('add',datum.value);
  56. taginput.typeahead('val','');
  57. }));
  58.  
  59. // erase any entered text on blur
  60. $(taginput).blur(function () {
  61. taginput.typeahead('val','');
  62. });
  63. });
  64.  
  65. });

猜你在找的jQuery相关文章