Select2 cancel/preventDefault select2:select at specific condition (v.4.0.x)

≡放荡痞女 提交于 2020-01-04 05:41:10

问题


I need to add a Button to every select2 item and prevent the default event so only the button gets triggered.

I have the following code but the normal onSelect event still gets triggered:

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
    }
}

回答1:


Try to catch select2:selecting event:

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

Here is a jsfiddle: http://jsfiddle.net/beaver71/dtjhpnm7/



来源:https://stackoverflow.com/questions/48079714/select2-cancel-preventdefault-select2select-at-specific-condition-v-4-0-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!