Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在select2上调用onclick事件_Javascript_Jquery_Onclick_Jquery Select2 - Fatal编程技术网

Javascript 如何在select2上调用onclick事件

Javascript 如何在select2上调用onclick事件,javascript,jquery,onclick,jquery-select2,Javascript,Jquery,Onclick,Jquery Select2,我正在尝试使用select2初始化将onclick绑定到select输入元素 我希望在单击或聚焦下拉列表时绑定处理程序 <select class="form-control" id="type" name="type" required="required"> <option value="breakfast" <?= ($type == 'breakfast')? 'selected' : '';?>>Breakfast</option>

我正在尝试使用select2初始化将onclick绑定到select输入元素

我希望在单击或聚焦下拉列表时绑定处理程序

<select class="form-control" id="type" name="type" required="required">
    <option value="breakfast" <?= ($type == 'breakfast')? 'selected' : '';?>>Breakfast</option>
    <option value="snacks" <?= ($type == 'snacks')? 'selected' : '';?>>Snacks</option>
</select>
我还尝试了“
选择2打开/打开
”,但没有成功。
如何使事件绑定工作?

在您使用的3.5.1版中,您应该使用
select2-selection
,而不是
select2:selection
,例如:

$('#type').select2({}).on("select2-selecting", function(e) {
    console.log('not working');
});
$('#type')。选择2({});
$('#type')。在(“select2 opening”上,函数(e){
控制台日志(“打开”);
});
$('#type')。打开(“select2 selection”,函数(e){
console.log('Selecting');
});

加利福尼亚
内华达州
俄勒冈
华盛顿

对于4.0+版,您可以在下面使用

$('#yourselect').on("select2:select", function(e) { 
   // what you would like to happen
});

您使用的版本是什么?@ZakariaAcharki我使用的是3.5.1version@Harish如果这起作用的话就不会发帖了。起作用了。。但我很确定在我将其更新为
select2:selection`…之前,它是
select2-selection'。。不管怎样,它现在正在工作。。谢谢
$('#yourselect').on("select2:select", function(e) { 
   // what you would like to happen
});