Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.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 选择2取消/预防默认选择2:在特定条件下选择(v.4.0.x)_Javascript_Events_Select_Jquery Select2 - Fatal编程技术网

Javascript 选择2取消/预防默认选择2:在特定条件下选择(v.4.0.x)

Javascript 选择2取消/预防默认选择2:在特定条件下选择(v.4.0.x),javascript,events,select,jquery-select2,Javascript,Events,Select,Jquery Select2,我需要为每个select2项目添加一个按钮,并阻止默认事件,以便只触发该按钮 我有以下代码,但仍会触发正常的onSelect事件: select.on('select2:select', test2); function test2(e) { if (e.params.originalEvent.target.classList.contains('TreeButton')) { //stop event execution e.stopPropagat

我需要为每个select2项目添加一个按钮,并阻止默认事件,以便只触发该按钮

我有以下代码,但仍会触发正常的
onSelect
事件:

select.on('select2:select', test2);

function test2(e) {
    if (e.params.originalEvent.target.classList.contains('TreeButton')) {
        //stop event execution
        e.stopPropagation();
        e.preventDefault();
        return false;
    } else {
        //execute normal
    }
}

尝试捕捉
select2:selection
事件:

select.on("select2:selecting", function (e) { 
    if (e.params.args.originalEvent.target.className === 'btn') {
      e.preventDefault();
    }
}
下面是一个JSFIDLE: