简体   繁体   English

javascript搜索功能

[英]javascript search functionality

I got an autocomplete search bar displaying the dropdown menu with images and text which is working good, but the problem is when there are no search results to display, the "No results found" text is also displaying with an image (like the actual results) and I know it is due to my _renderItem() method in jquery. 我有一个自动完成的搜索栏,其中显示带有图像和文本的下拉菜单,效果很好,但是问题是当没有搜索结果要显示时,“未找到结果”文本也会与图像一起显示(例如实际结果) ),我知道这是由于我在jQuery中的_renderItem()方法造成的。 I don't want it to display like that, but I just want the plain text saying "No results found" 我不希望它那样显示,但我只想说“找不到结果”的纯文本

I have been trying to figure it out but was unable to. 我一直在试图找出答案,但未能做到。 Any help is much appreciated. 任何帮助深表感谢。 Thanks. 谢谢。

This is javascript for displaying "no results found" (just the relevant part of code) 这是用于显示“未找到结果”的javascript(只是代码的相关部分)

success: function( data ) {
    if (data.length === 0) {
        data.push ({
        id: 0,
        label: "No results found"
        });
    }
   response( $.map( data, function( item ) {
      return {
        label: item.label,
        id: item.id

       };
   }));

And this is _renderItem() 这是_renderItem()

.data('autocomplete')._renderItem = function(ul, item) {            
         return $('<li>')                                  
        .data('item.autocomplete', item)
        .append("<a>"+"<img src ='/account/"+item.id+"/icon/logo' width='40' height='40'/>" + item.label+"</a>")
        .appendTo(ul);         
     };

And this is select() event 这是select()事件

select: function( event, ui ) { 
   window.location="/account/" + ui.item.id;
   return false;
}

How do I modify these two function in order to display just "No results found" text. 如何修改这两个功能,以便仅显示“未找到结果”文本。

I have tried using if (data.length > 0) {} but it doesn't seem to be working. 我试过使用if (data.length > 0) {}但它似乎没有用。

Since your only concern is of image, you can try something like this: 由于您只关心图像,因此可以尝试以下操作:

append("<a>"+"<img src ='/account/"+item.id+"/icon/logo' onerror='$(this).hide()' width='40' height='40'/>" + item.label+"</a>")

Adding an error attribute which hides the image on error. 添加错误属性以隐藏错误图像。 Since on no results found, you'll have a broken image which you easily hide using: 由于未找到结果,因此您将得到一个破碎的图像,可以使用以下方法轻松隐藏该图像:

onerror="$(this).hide()"

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

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