javascript Select2 关闭事件触发后聚焦输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24378615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Focusing Input after Select2 close event fire
提问by sobhan
I read always trigger "change"-event for <select>, even if the select2-option clicked is already selectedand 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.
我读到总是为 <select> 触发“更改”事件,即使单击的 select2 选项已经被选中,并且由于问题与我的问题非常接近,我在那里问了我的问题,但没有提供答案。我的问题是我有一个由 Select2 和文本输入构造的选择框。我想要实现的是在Select2 关闭后(无论值是否更改)我的文本输入成为焦点。
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/
http://jsfiddle.net/sobhanattar/x49F2/8/
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:
如您所见,文本输入不接受焦点。在对 Select2 文档进行一些挖掘并检查文档的 Events 部分之后,我意识到在 Close 事件之后,会自动触发一个 Focus 事件。这意味着在您关闭 Select2 选择框后,它会自动聚焦。所以我把我的代码改成了这样:
$('#select').select2({placeholder: 'City'});
$('#select')
.on('select2-close', function (e) {
$('#select').blur();
})
.on('select2-blur', function(e) {
$('#hey').focus();
});
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 事件顺序的理解是正确的还是我在中间错过了一些东西。
回答by ahmad molaie
take a look here, this question is already has an answer: Blur select2 input after close
看看这里,这个问题已经有了答案: 关闭后模糊选择2输入
.on("select2-close", function () {
setTimeout(function() {
$('.select2-container-active').removeClass('select2-container-active');
$(':focus').blur();
$("#elementid").focus();
}, 1);
});
elementid
is the id element you want to be focused after select2 closese.
i have just added $("#elementid").focus();
to the answer at link.
elementid
是您要在 select2 closese 后聚焦的 id 元素。我刚刚$("#elementid").focus();
在链接中添加了答案。
回答by Ravshanbek Ahmedov
Try this one as it works:
试试这个,因为它有效:
$('#select2').on('select2:close', function (e)
{
// your focus code for element
});
回答by dang.khoa.1989.2010
use css:
使用CSS:
html, body {
height:100%
}