Javascript jQuery UI 自动完成:清除上一个搜索词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8329697/
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
jQuery UI Autocomplete: Clear Previous Search Term
提问by Nick Olsen
On the keydown event of the jQuery UI Autocomplete widget the default case statement has the following code:
在 jQuery UI 自动完成小部件的 keydown 事件中,默认 case 语句具有以下代码:
default:
// keypress is triggered before the input value is changed
clearTimeout( self.searching );
self.searching = setTimeout(function() {
// only search if the value has changed
if ( self.term != self.element.val() ) {
self.selectedItem = null;
self.search( null, event );
}
}, self.options.delay );
break;
From what I can see the sixth line of the above code prevents execution if the user types in the same value twice. This makes sense most of the time but in my scenario it is causing a problem.
从我可以看到,如果用户两次输入相同的值,上面代码的第六行会阻止执行。大多数情况下这是有道理的,但在我的情况下,它会导致问题。
I am building an ajax application that has a fixed header with a search input at the top. The user can type in anything they want into the search box and a div will pop up with the search results matching their query. If the user types in 'abc' and clicks on a result, it loads the corresponding page via ajax and clears the search box. The problem is that if they user types in 'abc' again, the search method is not fired as what they typed in matches what they last searched for.
我正在构建一个 ajax 应用程序,它有一个固定的标题,顶部有一个搜索输入。用户可以在搜索框中输入他们想要的任何内容,然后会弹出一个 div,其中包含与他们的查询匹配的搜索结果。如果用户输入 'abc' 并点击结果,它会通过 ajax 加载相应的页面并清除搜索框。问题是,如果用户再次输入“abc”,则不会触发搜索方法,因为他们输入的内容与上次搜索的内容相匹配。
Is there a way to clear the self.term value stored within the widget when the user clicks on a search result? Or is there another way to resolve my problem without having to cook up my own version of the Autocomplete widget?
当用户点击搜索结果时,有没有办法清除存储在小部件中的 self.term 值?或者有没有另一种方法可以解决我的问题而不必编写我自己的自动完成小部件版本?
Thanks
谢谢
回答by Nick Olsen
Doing the following resets the previous search term:
执行以下操作会重置之前的搜索词:
$('#AutocompleteElementID').data().autocomplete.term = null;
If you do the following when an item is selected, it will clear the previous search term and make the widget perform a search if they type in the same value again.
如果您在选择项目时执行以下操作,它将清除之前的搜索词,并在再次输入相同值时使小部件执行搜索。
回答by Serj
Unfortunately,
很遗憾,
$(this).data().autocomplete.term = null;
does not clear the input field. To do so, the following works:
不清除输入字段。为此,以下工作:
close: function(event, ui) {
// Close event fires when selection options closes
$('input')[0].value = ""; // Clear the input field
}
回答by Hayden
This DID NOT work for me:
这对我不起作用:
$(this).data().autocomplete.term = null;
However this DID work for me in jQuery UI - v1.10.2:
然而,这在 jQuery UI - v1.10.2 中对我有用:
$(this).data().term = null;
回答by Apolo
You should try :
你应该试试 :
$("#AutocompleteElementID").autocomplete("instance").term = null;
Let me know if it works for you
请让我知道这对你有没有用
回答by Evan
if the autocomplete function returns self, for your autocomplete element you should just be able to call
如果自动完成功能返回 self,对于您的自动完成元素,您应该能够调用
$yourAutoCompleteElement.term = null;
when you want to clear it.
当你想清除它时。
回答by james burt
close: function(event, ui)
{
// Close event fires when selection options closes
$(this).data().autocomplete.term = null; // Clear the cached search term, make every search new
},
回答by Trevor
After Clearing the text field you can reset the previous search by invoking the jquery ui autocomplete "search" method with an empty string.
清除文本字段后,您可以通过使用空字符串调用 jquery ui 自动完成“搜索”方法来重置先前的搜索。
$('#AutocompleteElementID').autocomplete( "search", "" );
By default there is a minimum search length that has to be met before any items show up, so it effectively clears the search without displaying all the results.
默认情况下,在显示任何项目之前必须满足最小搜索长度,因此它可以有效地清除搜索而不显示所有结果。
http://api.jqueryui.com/autocomplete/#method-search
http://api.jqueryui.com/autocomplete/#method-search
This should be a stable way to handle this situation as the search method hasn't changed over time.
这应该是处理这种情况的稳定方法,因为搜索方法没有随着时间的推移而改变。
回答by Alan
The APIs have changed in the latest versions of jQuery/jQuery UI.
API 在最新版本的 jQuery/jQuery UI 中发生了变化。
The correct way of accessing the property on the widget instance now is:
现在访问小部件实例上的属性的正确方法是:
$("#AutocompleteElementID").autocomplete("instance").term = null;
Code example: http://jsfiddle.net/altano/sdpL17z5/
代码示例:http: //jsfiddle.net/altano/sdpL17z5/
Verified working with jQuery UI 1.11.4 and jQuery 2.2.3
经验证可使用 jQuery UI 1.11.4 和 jQuery 2.2.3