2011-01-19 40 views
4

新手編碼器在這裏。我有一個jQuery自動完成搜索欄,通過本地json數組搜索。當找不到匹配項時,我想返回一個字符串,表示「找不到」。jQuery UI自動完成:當沒有搜索匹配發生時返回「沒有找到」

我已經試過if語句裏面$ .grep但至今沒有奏效:

$("#div_name").autocomplete({ 
    appendTo: ".custom-autocomplete", 
    source: function (request, response) { 
    var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); 
    response($.grep(array, function(value) { 

    var not_found = 'Nothing found.'; 

    if (matcher.test(value.value).length && matcher.test(value.nickname).length == 0) { 
     return not_found; 
    } 
    else { 
    return matcher.test(value.value) 
     || matcher.test(value.nickname); 
    } 

    })); 
}, 

感謝您的幫助! :)

+0

想知道這一點。 – Koraktor 2011-03-21 07:19:48

回答

-1

你可以使用_renderItem來呈現你想要的結果。你只需要在這裏檢查結果是否爲空,然後做你想要的回報。

$("#div_name").autocomplete({ 
    ... 
}).data("autocomplete")._renderItem = function(ul, item) { 
    return "how and where you want to render results"; 
}; 
+2

如果沒有任何結果,「_renderItem」永遠不會被觸發。 – 2011-05-09 23:52:44

0

我想你想測試「或」(||)位置:

if (matcher.test(value.value).length && matcher.test(value.nickname).length == 0) { 
    return not_found; 
} 

如果兩個value.valuevalue.nickname沒有價值,只會是真實的。 (您還沒有表現出你的數據,所以我猜這裏)

if (matcher.test(value.value).length || matcher.test(value.nickname).length == 0) { 
    return not_found; 
} 

將匹配如果任是不正確的,立即救助,如果value.value確實有一個長度。

相關問題