如何触发多远程jquery自动完成UI的自动搜索选项
我正在使用这个自动完成库: http://jqueryui.com/demos/autocomplete/#multiple -remote
我想知道是否有人知道如何触发自动搜索而无需在文本框中输入任何内容。即如果我们想在按钮单击事件上显示列表。 “搜索”方法似乎就是这样做的。但我无法让它工作。
请注意,自动建议的数据来自网络服务。如果用户在文本框中键入内容,建议会相应更改。即键入的数据进入服务并带回建议。 输入的形式为“名称、位置”。因此,对于输入的不同部分,会显示不同类型的建议。
代码:
UI:
<div class="ui-widget">
<label for="birds">Birds: </label>
<input id="birds" size="50" />
</div>
脚本:
<script>
$(function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#birds" )
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( "search.php", {
term: extractLast( request.term )
}, response );
},
search: function() {
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
focus: function() {
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
terms.pop();
terms.push( ui.item.value );
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
</script>
如果我输入任何内容,它就会向 search.php 发送请求。 我尝试将逻辑“term.length < 2”更改为“term.length <= 0”。这可行,但我必须按空格键。然后在文本框中放置一个空白区域,并将请求发送到服务器。但我不想在那里输入任何内容。 希望有帮助。
I am using this auto complete library: http://jqueryui.com/demos/autocomplete/#multiple-remote
I was wondering if anyone knows how trigger the auto search without typing anything in the text box. i.e. if we want to display the list on a button click event.
the "search" method seems to be doing that. But I cant get it working.
Please note that the data of the auto suggest comes from a web service. If user types something in the text box the suggestion changes accordingly. i.e. the typed data goes to the service and brings back the suggestion.
The input is of "Name, Location" form. As a result for different parts of the input different types of suggestions are displayed.
Codes:
UI:
<div class="ui-widget">
<label for="birds">Birds: </label>
<input id="birds" size="50" />
</div>
Script:
<script>
$(function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#birds" )
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( "search.php", {
term: extractLast( request.term )
}, response );
},
search: function() {
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
focus: function() {
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
terms.pop();
terms.push( ui.item.value );
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
</script>
If I type anything it sends a request to search.php.
I tried changing the logic "term.length < 2" to "term.length <= 0". This works but i have to press the space bar. Then an empty space is placed in the text box and is sends the request to the server. But I don't want to type anything in there.
Hope that helps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想在单击按钮时触发搜索,则必须调用搜索方法。如果您想显示所有选项,请调用搜索并将值设置为空字符串,并将自动完成小部件设置为接受 minLength: 0。
因此,在代码中:
UI
脚本
这是您正在寻找的行为吗?
参考:http://jqueryui.com/demos/autocomplete/#method-search
If you want to trigger the search when a button is clicked, then you have to call the search method. If you want to show all options, call search with the value set to empty string and set the autocomplete widget to accept minLength: 0.
So in code:
UI
Script
Is this the behavior you are looking for?
Ref: http://jqueryui.com/demos/autocomplete/#method-search