简体   繁体   English

将焦点设置为jQuery UI MultiSelect Widget中的Filter输入

[英]Set focus to the Filter input in a jQuery UI MultiSelect Widget

I am writing a small script which will set focus to the filter text input field of the multi select jquery widget. 我正在编写一个小脚本,它将焦点设置到多选jquery小部件的过滤器文本输入字段。 Based on the documentation, I can subscribe to the click event of the widget like this: 根据文档,我可以订阅小部件的click事件,如下所示:

// bind to event
$("#multiselect").bind("multiselectopen", function(event, ui){
    // event handler here
});

So I tried this: 所以我尝试了这个:

$("#MyStatusWidget").bind("multiselectopen", function(event, ui){
            // event handler here
            $(this).$(".ui-multiselect-filter").contents('input :text').focus());
        });

Here is a link to the widget: http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/ 以下是该小部件的链接: http//www.erichynds.com/jquery/jquery-ui-multiselect-widget/

I also tried a couple other methods ($('').is(':text'); etc), but can't get the hook. 我还尝试了其他一些方法($('')。is(':text');等),但无法获得钩子。

Here is the HTML: 这是HTML:

<div class="ui-widget-header ui-corner-all ui-multiselect-header ui-helper-clearfix ui-multiselect-hasfilter">
<div class="ui-multiselect-filter">
Filter:
<input type="search" placeholder="Enter keywords">
</div>
<ul class="ui-helper-reset">
</div>

Thank you 谢谢

I know this is a little old, but in my case i had alot of these widgets on the page. 我知道这有点旧,但在我的情况下,我在页面上有很多这些小部件。 So I did something a little different that worked perfectly. 所以我做了一些与众不同的事情。

$("#MyStatusWidget").multiselect({
    open: function () {
        $(this).multiselect("widget").find("input[type='search']:first").focus();               
    }
});

When you create your multi select widget simply add the following "open" method. 创建多选小部件时,只需添加以下“打开”方法即可。

$("#MyStatusWidget").multiselect({
    open: function () {
        $("input[type='search']:first").focus();                   
    }
});

For IE10 bowser: 对于IE10 bowser:

$("#MyStatusWidget").multiselect({
    open: function () {
        $("input[type='text']:first").focus();                   
    }
});

I haven't tried the first two solutions to see if rrusnak's problems exist. 我没有尝试过前两个解决方案,看看是否存在rrusnak的问题。 My Solution doesn't have any of the problems rrusnak mentions about the others. 我的解决方案没有任何rrusnak提到的其他问题。 This one works with an unlimited number of Selectors on a page and uses simple jQuery along with Eric Hynds recommendations of implementing his multiselect filter system to the multiselect widget: 这个可以在页面上使用无限数量的选择器,并使用简单的jQuery以及Eric Hynds建议将多选过滤器系统实现到多选小部件:

$("#MyStatusWidget").multiselect({
open: function () {
  $(this).multiselectfilter("widget").find('input').focus();
  }
  }).multiselectfilter({
  label: '',
  autoReset: true
});

Its clean, can be chained with the other widget options and immediately allows text input without having to first click on the input filter. 它干净,可以与其他小部件选项链接,并立即允许文本输入,而无需先点击输入过滤器。

IMO Eric should have included an automatic focus in his filter script - as the use of a filter on the widget implies that it is to be used anyway. IMO Eric应该在他的过滤器脚本中包含一个自动焦点 - 因为在小部件上使用过滤器意味着无论如何都要使用它。 So having to manually focus to the input field is an unnecessary click for users. 因此,必须手动聚焦到输入字段是用户不必要的点击。

The above two answers worked for me, however there were a number of irritating things with the plugin that happen when the filter is focused. 以上两个答案对我有用,但是当过滤器聚焦时,插件会发生一些令人恼火的事情。 Most notably you can no longer use the arrow keys to select an option, which really takes away keyboard control. 最值得注意的是,您不能再使用箭头键选择一个选项,这实际上会消除键盘控制。

I have implemented a number of changes, that you can find at my github link below. 我已经实现了一些更改,您可以在下面的github链接中找到它们。

  1. Fixes tabbing issues with the form 修复了表单的标签问题
  2. Allows use of the arrow keys to select options while the filter is selected 在选择过滤器时,允许使用箭头键选择选项
  3. I have changed the traverse function to be friendly towards filtered lists. 我已将遍历功能更改为对筛选列表友好。
  4. I have changed the traverse function to move up one selection at a time when using the up arrow (instead of going to the top of the list) 我已经改变了遍历功能,使用向上箭头一次向上移动一个选项(而不是转到列表的顶部)
  5. Pressing 'f' (or 'ctrl-f') will re-focus on the filter box 按'f'(或'ctrl-f')将重新聚焦在滤镜盒上

Hopefully this helps anyone who has had some of the same issues as myself. 希望这可以帮助那些遇到与我一样的问题的人。 The changes are in both the regular multiselect as well as the filter src files. 这些更改包括常规multiselect和filter src文件。

https://github.com/rrusnak1/jquery-ui-multiselect-widget https://github.com/rrusnak1/jquery-ui-multiselect-widget

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM