[英]how to remove bootstrap tags input Events from the original element
bootstrap-tagsinput中提供了将事件绑定到原始元素(输入元素)的功能。
例如:
$('input').on('beforeItemRemove', function(event) {
// event.item: contains the item
// event.cancel: set to true to prevent the item getting removed
});
但是,如果我destroy
了tagsinput
并像下面那样再次tagsinput
进行初始化,
$('input').tagsinput('destroy');
$('input').tagsinput();
beforeItemRemove
事件将触发2次。 如果我再次删除了tagsinput
功能,然后再次初始化。 当我删除另一个标签之前, beforeItemRemove
将触发3次。 当我删除并添加标签输入功能时,这种情况一直在发生。
为什么会这样呢?
尝试使用delegate
$(document).on('beforeItemRemove', 'input', function(event) {
// event.item: contains the item
// event.cancel: set to true to prevent the item getting removed
});
不过,我只是转到您提供的链接,将您的处理程序附加到开发人员控制台中,然后destroy
/ initialzer
,它只为我触发一次。 因此,可能是代码中的其他内容导致了问题。
根据其他评论和答案,我找到了避免这种现象的方法。 我编辑了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.