gpt4 book ai didi

javascript - Selectize.js 委托(delegate)事件选项不起作用

转载 作者:行者123 更新时间:2023-11-30 15:35:49 26 4
gpt4 key购买 nike

我目前正在尝试监听下拉列表中生成的选项的点击事件,但我目前无法实现。

由于加载 DOM 时不会生成选项,因此我将事件委托(delegate)给监听下拉选项点击的文档。我的代码目前看起来像这样:

var $accountsSelectize = $('#accounts-input').selectize({
...
});

$(document).on('click', '.accounts-input-selectize .option', function(event) {
alert('An option was clicked!');
});

但这似乎不起作用。知道为什么会这样吗?任何有关此事件为何根本未触发的线索都将非常受欢迎。

编辑:仅供引用,我正在向 HTML 输入元素添加一个类,这就是为什么我要监听对 .accounts-input-selectize .option 的点击:

<input type="text" id="accounts-input" class="accounts-input-selectize" placeholder="Search accounts">

最佳答案

这个问题听起来很简单,其实不然。问题是 selectize 会阻止所有默认设置,所以一开始我会推荐使用这个库,但如果你真的想要的话,还是有办法查明用户是否更改了选项。

$('#select-country').selectize({

//When user selects the widget we'll rememberize its value
onFocus: function(){
$(this).data('temp-saved', $('#select-country').val());
},

//On blur we check if the value has not changed
onBlur: function(){
var previous = $(this).data('temp-saved');
var current = $('#select-country').val();

if (current == previous) {
console.log('NOT changed!');
}
},

//And on change we sure that the value has changed
onChange: function(current){
var previous = $(this).data('temp-saved');
console.log('changed from', previous, 'to', current);
}
});

https://jsfiddle.net/mo1Ljm7r/13/

关于javascript - Selectize.js 委托(delegate)事件选项不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41557016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com