2016-12-05 29 views

回答

4

嗯,我看着你的代碼。由於您正在使用keyup事件來調用doSearch函數,同時您使用的是用於隱藏/顯示數據的fadeIn函數。我想用一個示例來說明問題,當您鍵入任何字符時它將隱藏所有文本(對於一個很小的範圍的時間),然後顯示結果(基於doSearch函數)。所以,當你輸入fast時,每次調用doSearch函數時都會調用doSearch函數,因此它會在空文本上調用doSearch函數(因爲前面的調用不會返回結果,因爲i-e文本仍然隱藏)。也許你需要綁定你的doSearch函數和某種限制,直到文本沒有被顯示,它將不會被調用。編輯:只需更改行數:
CharityNames.parents(「li」)。fadeIn(); CharityText.parents(「li」)。fadeIn(); 至 CharityNames.parents(「li」)。show(); CharityText.parents(「li」)。show();

它會正常工作。但我不知道這是否是你的要求淡入文本。

+0

@Athul朱奈德·艾哈邁德__did__提出一個合適的回答,一個在其答案對應的代碼__is__!我只是測試它在你的http://codepen.io/anon/pen/ObvPmw,它工作正常。 – cFreed

0

這是一個建議,而不是你問題的解決方案,詢問爲什麼你的代碼不能按照你的預期工作。

你的邏輯看起來並不複雜,而且太多的函數調用會降低性能。

如果有興趣,下面是一個小樣本,它完全符合您的需求。 Working Example

//This is a extension to do case insensitive search. Extension Name "containsCIn" 
$.extend($.expr[':'], { 
    'containsCIn': function(elem, i, match, array) { 
    return (elem.textContent || elem.innerText || '').toLowerCase() 
     .indexOf((match[3] || "").toLowerCase()) >= 0; 
    } 
}); 

// this is the sample code which filters the content on search string 
$(function() { 
    $('#txtesearch').on('keyup', function() { 
    var searchtext = $(this).val(); 

    $('ul.charity-listing, ul.charity-listing li').hide(); 
    $('ul.charity-listing li:containsCIn("' + searchtext + '")').show(); 

    $('ul.charity-listing').fadeIn({duration: 200}); // default is 400, i reduced the time to make it render fast. 
    }); 
});