正在获取DOM中的所有select2实例
Getting all select2 instance in DOM
当select2是焦点时,我试图解除键的绑定,这似乎能在中工作
$('#someId').on('select2-focus',function(){
$(document).unbind('keypress');
});
但我想获得所有select2实例,我已经尝试过
$('.select2').on('select2-focus',function(){
$(document).unbind('keypress');
});
但它不起作用,我不想在我的页面中的每个select2中添加".custom_class"。有人能帮我做这个吗?提前谢谢。
如果有人仍然对原始问题的答案感兴趣,即"在DOM中获取所有select2实例":
// The $element can be a narrower scope, or the entire document.
$element = $(document);
$element.find('select').each(function (i, select) {
var $select = $(select);
// Make sure that the select has Select2.
if($select.hasClass("select2-hidden-accessible")) {
// Perform your action, e.g.:
$select.on('select2-focus',function(){
$(document).unbind('keypress');
});
// You can also use the console for debugging:
// console.log($select);
}
});
$select.hasClass("select2-hidden-accessible")
检查取自原始文档:https://select2.org/programmatic-control/methods#checking-如果插件已初始化
但请注意,"select2焦点"事件可能不再存在,因为此处未列出:https://select2.org/programmatic-control/events。你应该能够使用$select.find('.select2-selection__rendered').on('focus', function ...)
,但我没有测试。
有关如何使用它的工作示例,请参阅我的jsfiddle中的Select2问题。注意function unobscurePlaceholdersOfSelect2()
。
jQuery('.select2-container').each(function(i,el){
$(el).data('select2').close()
})
我不知道你的html是什么。但试试看?jQuery( "[attribute='value']" )
例如:$("input[type='text']")
相关文章:
- ES6构造函数返回基类的实例
- KnockoutJS-组件-多个实例
- 为什么无法在TypeScript中导出类实例
- 如何在GoogleWeb工具包(GWT)中从JSNI调用接口(实例化)
- 阻止在select2单击时调用ajax
- fluxxor向一个flux实例添加一组以上的操作
- 使用jQuery获取Dropzone实例/对象
- "实例范围”;TypeScript类的getter/setter
- 如何在速度模板中获取LiferayPortlet实例id
- 同一项怎么可能在一个实例中未定义,却在另一个实例上定义
- 实例创建(JS)
- 显示模块模式在Knockout中设置模型的新实例
- 如何获取单选按钮的多个实例的选定单选按钮值
- 当同一浏览器的两个实例浏览时,Javascript页面如何具有唯一的ID
- 在哪里可以找到RXUI Javascript'时间飞逝'实例
- spine.js在启动时填充模型实例
- 不再需要时使用jQuery/kill实例
- Ajax模式下的Jquery Select2 V4在IE9中不起作用
- 如何在用户输入时实例化数组
- 正在获取DOM中的所有select2实例