自动完成和bootstrap组合框

时间:2016-12-06 13:46:23

标签: javascript jquery autocomplete

我有bootstrap组合框,在自动完成更改时我要选择值,问题是这只有在我双击组合中的过滤结果时才有效,如果我只点击一次我将myselection变量变为空。

$('#MyComboId').on('change', function () {
     var myselection = $(this).find("option:selected").val();          
  });    
});

1 个答案:

答案 0 :(得分:0)

如果我正确理解你使用bootstrap-comboboxLink)。

我找到了setter代码:

select: function () {
  var val = this.$menu.find('.active').attr('data-value');
  this.$element.val(this.updater(val)).trigger('change');
  this.$target.val(this.map[val]).trigger('change');
  this.$source.val(this.map[val]).trigger('change');
  this.$container.addClass('combobox-selected');
  this.selected = true;
  return this.hide();
}

更新:

当用户点击下拉列表中的项目时,change事件将触发,这是不正确的。即输入将被清除,之后将填充正确的值。

var el = $('.combobox').combobox();

el.on('change', function(e){
 if($(this).data('combobox').$element.val() == ''){
   console.log('Its triggered incorrectly');
   return false;
 }

 var dic = $(this).data('combobox').map,
    val = $(this).val(),
  clean = true;

 for(var i in dic){
   if(!dic.hasOwnProperty(i)) continue;
   if(dic[i] == val){
     console.log(dic[i], i); // dic[i] = value, i = label
     clean = false;
     break;
   }
 }

 if(clean)
   console.log('Input was cleared');
})

Demo

相关问题