关闭后模糊Select2输入 [英] Blur Select2 input after close

查看:126
本文介绍了关闭后模糊Select2输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望select2元素在触发select2-close事件时失去焦点,到目前为止我尝试的是:

I want the select2 element to lose the focus when the select2-close event is triggered, what I have tried so far was:

.on("select2-close", function (e) {
    var $focused = $(':focus');
    $focused.blur();
});

以及获取焦点元素的一些变体,例如 document.activeElement ,$(e.target)非这些工作。

and some variations to get focused element like document.activeElement, $(e.target) non f these worked.

JSFiddle

推荐答案

您需要删除 .select2-container-active 来自包含分隔符的类:

You need to remove the .select2-container-active class from the containing divider:

.on("select2-close", function () {
    setTimeout(function() {
        $('.select2-container-active').removeClass('select2-container-active');
        $(':focus').blur();
    }, 1);
});

我在这里使用了 setTimeout 一种hacky方法,确保在插件本身完成关闭后触发。

I've used a setTimeout here as a hacky way to ensure that this triggers after the plugin itself finalises the close.

JSFiddle演示

这篇关于关闭后模糊Select2输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆