简体   繁体   English

如何从原始元素中删除引导程序标签输入事件

[英]how to remove bootstrap tags input Events from the original element

In bootstrap-tagsinput there is a functionality to bind events to the original element(input element). bootstrap-tagsinput中提供了将事件绑定到原始元素(输入元素)的功能。

ex : 例如:

$('input').on('beforeItemRemove', function(event) {
   // event.item: contains the item
   // event.cancel: set to true to prevent the item getting removed
});

But if I destroy the tagsinput and initialize it again like below, 但是,如果我destroytagsinput并像下面那样再次tagsinput进行初始化,

$('input').tagsinput('destroy');
$('input').tagsinput();

beforeItemRemove event will fire 2 times. beforeItemRemove事件将触发2次。 If I remove the tagsinput functionality again and initialize again. 如果我再次删除了tagsinput功能,然后再次初始化。 When I remove another tag beforeItemRemove will fire 3 times. 当我删除另一个标签之前, beforeItemRemove将触发3次。 This keeps happening when I remove and add the tagsinput functionality. 当我删除并添加标签输入功能时,这种情况一直在发生。

Why is this happening? 为什么会这样呢?

try using delegate 尝试使用delegate

$(document).on('beforeItemRemove', 'input', function(event) {
   // event.item: contains the item
   // event.cancel: set to true to prevent the item getting removed
});

though, I just went to the link you provided, attached your handler in the developer console and destroy / initialzer and it only fired once for me. 不过,我只是转到您提供的链接,将您的处理程序附加到开发人员控制台中,然后destroy / initialzer ,它只为我触发一次。 Therefore it could be something else in your code which is causing you the issue. 因此,可能是代码中的其他内容导致了问题。

Based on the other comments and answers I found a way to avoid this behavior. 根据其他评论和答案,我找到了避免这种现象的方法。 I edited the Destroy function. 我编辑了Destroy功能。

/**
 * Removes all tagsinput behaviour and unregsiter all event handlers
 */
destroy: function() {
  var self = this;

  // Unbind events
  self.$container.off('keypress', 'input');
  self.$container.off('click', '[role=remove]');

    // unbind the additional events

    self.$element.off('itemAdded');
    self.$element.off('beforeItemAdd');   
    self.$element.off('itemRemoved');     
    self.$element.off('beforeItemRemove');    

  self.$container.remove();
  self.$element.removeData('tagsinput');
  self.$element.show();
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM