javascript select2 - 获取当前搜索文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21097537/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:02:56  来源:igfitidea点击:

select2 - get current search text

javascriptjquery-select2

提问by Adam Rackis

I'm using select2 and fetching available options from the server with this query function

我正在使用 select2 并使用此查询功能从服务器获取可用选项

var recipientsTimeout;
this.loadRecipients = function (query) {
    clearTimeout(recipientsTimeout);
    recipientsTimeout = setTimeout(function(){
        data.transmitRequest('lookupcontacts', { search: query.term }, function(resp){
            query.callback({ results: resp.items });
        });
    }, 500);
};

It uses our own ajax layer, and delays searching until the user stops typing, and it works fine. The only small issue is that if the user types some text, then immediately backspaces over it, my last timeout will still fire, and an ajax request will be fired, and ignored. This doesn't cause any problems, but it's less than optimal.

它使用我们自己的 ajax 层,并延迟搜索直到用户停止输入,并且它工作正常。唯一的小问题是,如果用户输入一些文本,然后立即退格,我的最后一次超时仍然会触发,并且会触发 ajax 请求,并被忽略。这不会导致任何问题,但它不是最佳的。

Is there any (preferably non-brittle) way to fetch whatever the current text is? It seems as though the queryobject sent in has an element property, which is a jQuery wrapper of the original hidden input I set select2 up with, but there's no direct way I can see to get the actual textbox that the user is typing in. Obviously I could inspect it and easily figure out the dom pattern and build up a selector from the hidden element back to what the user is typing in, but I really hate doing that, since the specific layout could easily change in a future version.

是否有任何(最好是非易碎的)方法来获取当前文本是什么?似乎query发送的对象有一个元素属性,它是我设置 select2 的原始隐藏输入的 jQuery 包装器,但我无法直接看到获取用户正在输入的实际文本框。显然我可以检查它并轻松找出 dom 模式并构建一个从隐藏元素返回到用户输入内容的选择器,但我真的很讨厌这样做,因为特定的布局在未来的版本中很容易改变。

So what is the best way to get the currently entered text, so I could do a quick check on it when the setTimeout expires, and I'm about to run my ajax request.

那么获取当前输入的文本的最佳方法是什么,以便我可以在 setTimeout 到期时对其进行快速检查,并且我将要运行我的 ajax 请求。

采纳答案by Jan

The hidden input element (where you initialize select2 on) gets a data property select2, which contains references to all elements, that are used by select2. So you could do something like this:

隐藏的输入元素(在其上初始化 select2)获得一个数据属性select2,其中包含对 select2 使用的所有元素的引用。所以你可以做这样的事情:

var hiddenInputSelector = '#e1',
    select2 = $(hiddenInputSelector).data('select2'),
    searchInput = select2.search;

if($(searchInput).val() === '')
    clearTimeout(recipientsTimeout);

This is not in the docs though, so it might change in the future.

但这不在文档中,因此将来可能会更改。

回答by Javier

I'm using 4.0.3 and the way I did it is this:

我使用的是 4.0.3,我的做法是这样的:

$("#mySelect2").data("select2").dropdown.$search.val()

回答by eeeeeean

In select2 version 4.0.3this works for me, whereas the others did not:

在 select2 版本4.0.3这对我有用,而其他人则没有:

$('.select2-search__field')[0].value;

回答by Chad Kuehn

I do something like this to get the current search term:

我做这样的事情来获取当前的搜索词:

list.data("select2").search[0].value;