我们如何防止禁用的 Select2 下拉菜单聚焦于点击。可以很好地防止将注意力集中在按“TAB”键上。
我已经尝试将 tabindex='-1' 添加到 Select2 层次结构中的各种 div 元素。没有帮助。
例如,引用this link转到他们的文档页面并尝试单击禁用的 Select2 示例,它将获得焦点,这在我的情况下不应该发生。
编辑:
尝试了@Guruprasad 对上述链接的回答,但在 Chrome 的控制台上使用以下代码没有成功:
jQuery('.select2-container--disabled .select2-selection').on('click', function () {
console.log('clicked');
jQuery('.select2-selection').blur;
});
每次我点击禁用的选择时都会打印“clicked”,但它不会移除元素的焦点。
最佳答案
就写这个JS
$('.select2-selection').on('click',function(){
$(this).blur();
});
更新
blur
应该是 blur()
jQuery('.select2-container--disabled').on('click', function () {
console.log('clicked');
jQuery('.select2-selection').blur();
});
更新 2:
我很难说你是否可以停止获得焦点,但是为了 ui
透视图向用户显示它已被禁用,你可以在 css
下方添加:
.select2-container--default .select2-selection--single .select2-selection__rendered
{
cursor:not-allowed;
}
关于javascript - 选择 2 : Prevent Select2 dropdown from getting focused on click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30621803/