admin管理员组文章数量:1332382
I tried to disable auto focus of input search inside select2
especially on mobile to disable keyboard popup. However, as documented here:
select2 will not be triggering the native events. select2 will also not be triggering non-native versions of the events, which is less of an issue as we still have the option to add the native events without breaking patibility.
So the only way I could do is to try to get every input box inside select2 that was currently on focused and set lose focus, but has no luck.
$("select").select2().on("select2-open",":input",function(){
setTimeout(function(){
$(":focus").blur();
}, 50);
});
Is there any possibility that I could achieve that result above? Thanks.
I tried to disable auto focus of input search inside select2
especially on mobile to disable keyboard popup. However, as documented here:
select2 will not be triggering the native events. select2 will also not be triggering non-native versions of the events, which is less of an issue as we still have the option to add the native events without breaking patibility.
So the only way I could do is to try to get every input box inside select2 that was currently on focused and set lose focus, but has no luck.
$("select").select2().on("select2-open",":input",function(){
setTimeout(function(){
$(":focus").blur();
}, 50);
});
Is there any possibility that I could achieve that result above? Thanks.
Share edited Oct 11, 2018 at 7:26 vrintle 5,5963 gold badges18 silver badges47 bronze badges asked Oct 11, 2018 at 7:21 Houy NarunHouy Narun 1,7259 gold badges42 silver badges95 bronze badges 2- Duplicate: stackoverflow./questions/17995057/…. – andreivictor Commented Oct 11, 2018 at 7:39
- @andreivictor, the answers there does not solve my problem, target version is v4 while I'm using select2 v3, thanks – Houy Narun Commented Oct 11, 2018 at 7:49
5 Answers
Reset to default 2Finally, I managed to find solution which works just fine for me as below:
/* Hide keyboard on select2 open event */
function hideSelect2Keyboard(e){
$('.select2-search input, :focus,input').prop('focus',false).blur();
}
$("select").select2().on("select2-open", hideSelect2Keyboard);
$("select").select2().on("select2-close",function(){
setTimeout(hideSelect2Keyboard, 50);
});
Tested on Tablet, and iOS device. In function hideSelect2Keyboard()
, I searched for every current focus element, include input field which could be used to initialized select2, setting .prop('focus',false)
which will remove focus and consequently disable keyboard popup on select2-open
and select2-close
event, by chaining .blur()
is to remove focus border from element. Then I attached this function to select event open
and close
and it works just fine.
I hope this will help other who searching for this as me too. Thanks.
I think I've found a solution for select v3
- tested in v3.5.4
.
We can use the option shouldFocusInput
, which must be a function that should return true
or false
.
So initialize the plugin with the following code:
$(document).ready(function() {
$('select').select2({
shouldFocusInput: function() {
return false;
}
});
});
Codepen demo: https://codepen.io/andreivictor/pen/JmNzvb
If you want to disable the auto-focus only on mobile devices, my approach is to use Modernizr library, which can test for the existence of Touch Events in the browser.
So the plete code should be:
$(document).ready(function() {
$('select').select2({
shouldFocusInput: function() {
if (Modernizr.touch) {
return false;
}
return true;
}
});
});
this is in the top google results for "select2 disable focus when cleared"
here is the solution for Select2 4.0.0 | Select2 4.1.1
$("select").on("select2:clear", function (evt) {
$(this).on("select2:opening.cancelOpen", function (evt) {
evt.preventDefault();
$(this).off("select2:opening.cancelOpen");
});
});
Source Thread: https://github./select2/select2/issues/3320#issuement-524153140
jump to [kevin-brown - Aug 22, 2019]
seo tldr;
- clear button in select2 4.x causes focus
- stop select2 from regaining focus after clear
- select2 disable focus when cleared
select2 version 4.0.11
This is what worked for me:
$('.select2').on('select2:opening', function (e) {
// Remove focus from the input when opening
setTimeout(function() {
$('.select2-search__field').blur();
}, 1);
});
I am not sure why, but the above solutions didn't work for me. But this one worked-
$('select').on('select2:open', function (event) {
$('.select2-search input').prop('focus',false);
});
本文标签: javascriptDisable auto focus on dropdown open of select2Stack Overflow
版权声明:本文标题:javascript - Disable auto focus on drop-down open of select2? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742293989a2448350.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论