[英]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”。 有人可以帮我吗? 提前致谢。
jQuery('.select2-container').each(function(i,el){
$(el).data('select2').close()
})
如果有人仍然对原始问题的答案感兴趣,即“在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 : $select.hasClass("select2-hidden-accessible")
但请注意,“ select2-focus”事件可能不再存在,因为它未在此处列出: https : //select2.org/programmatic-control/events 。 您应该能够使用$select.find('.select2-selection__rendered').on('focus', function ...)
,但是我没有对此进行测试。
有关如何使用它的工作示例,请参阅我的jsfiddle中的Select2问题 。 注意function unobscurePlaceholdersOfSelect2()
。
我不知道你的HTML是什么。 但是尝试一下? jQuery( "[attribute='value']" )
例如: $("input[type='text']")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.