javascript - Getting all select2 instance in DOM - Stack Overflow

Im trying to unbind keypress when select2 is focus, this seems to work$('#someId').on('s

Im trying to unbind keypress when select2 is focus, this seems to work

 $('#someId').on('select2-focus',function(){
    $(document).unbind('keypress');
});

but i want to get all select2 instance, i have tried

 $('.select2').on('select2-focus',function(){
    $(document).unbind('keypress');
});

but it is not working, i dont want to add ".custom_class" in each select2 i have in my page. can someone help me on this one? Thanks in advance.

Im trying to unbind keypress when select2 is focus, this seems to work

 $('#someId').on('select2-focus',function(){
    $(document).unbind('keypress');
});

but i want to get all select2 instance, i have tried

 $('.select2').on('select2-focus',function(){
    $(document).unbind('keypress');
});

but it is not working, i dont want to add ".custom_class" in each select2 i have in my page. can someone help me on this one? Thanks in advance.

Share Improve this question edited Dec 10, 2015 at 2:48 RobG 148k32 gold badges179 silver badges214 bronze badges asked Dec 10, 2015 at 2:19 jay tempjay temp 1,20312 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

In case anyone was still interested in the answer for the original question, i.e., „Getting all select2 instance in DOM“:

// 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);
    }
});

The $select.hasClass("select2-hidden-accessible") check is taken from the original docs: https://select2/programmatic-control/methods#checking-if-the-plugin-is-initialized

But notice, that the 'select2-focus' event might not exist anymore, as it is not listed here: https://select2/programmatic-control/events . You should be able to use $select.find('.select2-selection__rendered').on('focus', function ...), but I did not test that.

For a working example of how this can be used, see my jsfiddle for a Select2 issue. Notice the function unobscurePlaceholdersOfSelect2().

jQuery('.select2-container').each(function(i,el){
    $(el).data('select2').close()
})

I don't know what's your html. but try it? jQuery( "[attribute='value']" )

for example: $("input[type='text']")

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744868785a4598133.html

相关推荐

  • javascript - Getting all select2 instance in DOM - Stack Overflow

    Im trying to unbind keypress when select2 is focus, this seems to work$('#someId').on('s

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信