Javascript 如何删除自动完成搜索字段中出现的“未定义”文本?
我有一个自动完成的搜索。如果没有与搜索对应的结果,则会显示消息no results 如果用户输入了3个与任何结果都不对应的字母,则消息不会有结果 但是,它在消息“无结果”上方显示为未定义。你知道如何删除未定义的文本吗Javascript 如何删除自动完成搜索字段中出现的“未定义”文本?,javascript,jquery,Javascript,Jquery,我有一个自动完成的搜索。如果没有与搜索对应的结果,则会显示消息no results 如果用户输入了3个与任何结果都不对应的字母,则消息不会有结果 但是,它在消息“无结果”上方显示为未定义。你知道如何删除未定义的文本吗 $.widget( "custom.catcomplete", $.ui.autocomplete, { _create: function() { this._super(); this.widget().menu( "option", "
$.widget( "custom.catcomplete", $.ui.autocomplete, {
_create: function() {
this._super();
this.widget().menu( "option", "items", "> :not(.ui-autocomplete-category)" );
},
_renderMenu: function( ul, items ) {
var that = this,
currentCategory = "";
$.each( items, function( index, item ) {
var li;
if ( item.category != currentCategory ) {
ul.append( "<li>" + item.category + "</li>" );
currentCategory = item.category;
}
li = that._renderItemData( ul, item );
if ( item.category ) {
li.attr( "aria-label", item.category + " : " + item.label );
}
});
}
});
$("#search").catcomplete({
source: "{{ URL::to('autocomplete-search') }}",
minLength: 3,
response: function(event, ui){
if (!ui.content.length) {
var noResult = { value:"", label:"No results." };
ui.content.push(noResult);
}
},
select: function(event, ui) {
if(ui.item.category=="Conferences"){
window.location.href = ui.item.url;
}
else{
$.get(ui.item.url, function(result) {
var newConferences = '';
var placeholder = "{{route('conferences.show', ['id' => '1', 'slug' => 'demo-slug'])}}";
$.each(result, function(index, conference) {
var url = placeholder.replace(1, conference.id).replace('demo-slug', conference.slug);
newConferences += '<div>\n' +
' <div class="card box-shaddow">\n' +
' <div class="card-body">\n' +
' <h5>'+conference.name+'</h5>\n' +
' </div>\n' +
' </div></div>';
});
$('#conferences').html(newConferences);
}, 'json');
}
}
});
如果item.category!=当前类别,条件为item.category&&item.category!=当前类别
这样,当项目没有属于您的类别时:“undefined”不会在html中添加无用的行。您收到的内容可能与生成列表“+conference.name+”时预期的结构不符。您是否尝试过调试该部分或输出conference变量以查看服务搜索调用返回的结果?谢谢,问题是,如果有与搜索对应的结果,则会显示Conferences,下面会显示与搜索对应的Conferences。但是,如果用户执行的搜索与任何结果都不对应,则会显示消息No results,但是如果搜索返回的结果显示Undefined,则会显示在显示会议的位置。