在Select2关闭事件触发后聚焦输入 [英] Focusing Input after Select2 close event fire
问题描述
我读了总是为< select>触发更改 - 事件,即使已经选中了单击的select2选项并且由于问题非常接近我的问题,我在那里问了我的问题,但没有提供答案。我的问题是我有一个由Select2构造的选择框和一个文本输入。我想要实现的是 Select2关闭(无论值是否更改)我的文字输入变为焦点。
I read always trigger "change"-event for <select>, even if the select2-option clicked is already selected and as the problem was so close to my issue I asked my question there but no answer provided. My problem is that I have a select-box constructed by Select2 and a text input. What I want to achieve is after Select2 closed (no matter the value is changed or not) my text input become focus.
所以我做了一些逻辑上很好的事情如下:
So I did something logically fine as follow:
$('#select').select2({placeholder: 'City'});
$('#select')
.on('select2-close', function (e) {
$('#hey').focus();
});
http://jsfiddle.net/sobhanattar/x49F2/8/
正如您所看到的,文本输入不是接受焦点。在对Select2文档进行一些挖掘并检查文档的事件部分后,我意识到在Close事件之后,会自动触发Focus事件。这意味着在关闭Select2选择框后,它会自动聚焦。所以我将我的代码更改为:
As you can see, the text input doesn't accept focus. After some digging in Select2 document and inspecting Events part of documentation, I realized that after Close event, there is a Focus event fire automatically. It means that after you close a Select2 select-Box, it focuses itself. So I changed my code to something like this:
$('#select').select2({placeholder: 'City'});
$('#select')
.on('select2-close', function (e) {
$('#select').blur();
})
.on('select2-blur', function(e) {
$('#hey').focus();
});
它运作正常。现在我想知道我对Select2事件顺序的理解是正确的,或者我错过了中间的东西。
And it works just fine. Now I want to know that my understanding from Select2 order of events was correct or I missed something in the middle.
推荐答案
拿一个看这里,这个问题已经有了答案:
关闭后模糊select2输入
take a look here, this question is already has an answer: Blur select2 input after close
.on("select2-close", function () {
setTimeout(function() {
$('.select2-container-active').removeClass('select2-container-active');
$(':focus').blur();
$("#elementid").focus();
}, 1);
});
elementid
是你想要的id元素在select2 closese之后关注。
我刚刚将 $(#elementid)。focus();
添加到链接的答案。
elementid
is the id element you want to be focused after select2 closese.
i have just added $("#elementid").focus();
to the answer at link.
这篇关于在Select2关闭事件触发后聚焦输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!