Twitter Bootstrap 3 Typeahead / Tagsinput Completing Twice

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 16:31:22

I'm not sure this is looking bug, nothing custom code inside function, but selected tag is repeated in input field, but you can use alternative solution, itemAdded event to remove selected value from input field, see below sample code.

$('input').tagsinput({
  typeahead: {
    source: ['Amsterdam', 'Washington', 'Sydney', 'Beijing', 'Cairo']
  },
  freeInput: true
});
$('input').on('itemAdded', function(event) {
    setTimeout(function(){
        $(">input[type=text]",".bootstrap-tagsinput").val("");
    }, 1);
});

I have also noticed the input field is generating every tag section so this or event couldn't be targeted tag input field, due to dynamic generation input field you will also need to delay to select input element from <selector>

DEMO

UPDATE : $.get() function is return xhr object not server response, so you need add callback method to get AJAX response, see below sample code.

$('.tagsInput').tagsinput({
      confirmKeys: [13, 44],
      maxTags: 1,
      typeahead: {                  
          source: function(query) {
            return $.get('listcategories.php').done(function(data){
              /*if you have add `content-type: application/json` in 
                server response then no need to parse JSON otherwise,
                you will need to parse response into JSON.*/
              return $.parseJSON(data);
            })
          }
      }
 });

A less verbose solution using afterSelect option:

$(".tags").tagsinput({
     typeahead: {
        afterSelect: function(val) { this.$element.val(""); },
        source: keywords
     }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!