“No Matches” message for jquery ui autocomplete

柔情痞子 提交于 2019-12-07 04:37:34

问题


I'm new to this and have looked at other examples but am unclear on how to set a "No Matches" messages for the latest version of autocomplete http://docs.jquery.com/UI/Autocomplete when there are no results.

This is the code I'm using, can someone help write the rest, ideally keeping it clickable to a 'suggestions' page.

<script>
    $(document).ready(function() {
        var data = [
            {label: 'Yahoo', value: 'http://yahoo.com'},
            {label: 'BMW', value: 'http://bmw.com'},
            {label: 'Bing', value: 'http://bing.com'}
        ]; 
            $("input#autocomplete").autocomplete({
                source: function(request, response) {
                var results = $.ui.autocomplete.filter(data, request.term);
                response(results.slice(0, 10))},            
            focus: function (event, ui) {
                $(event.target).val(ui.item.label);
                return false;
            },
            select: function (event, ui) {
                $(event.target).val(ui.item.label);
                window.location = ui.item.value;
                return false;
            }
        });
    });
  </script>

Thanks in advance.

UPDATE: Have managed to get a fix together, but how can I embed a working link within the message?

<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
        var data = [
                {label: 'Yahoo', value: 'http://yahoo.com'},
                {label: 'BMW', value: 'http://bmw.com'},
                {label: 'Bing', value: 'http://bing.com'}
        ]; 
                $("input#autocomplete").autocomplete({
                source: function(request, response) {
                var results = $.ui.autocomplete.filter(data, request.term);
                if (!results.length) {
                            $("#no-results").text("<a href=\"/\">No results found!</a>");
                        } else {
                            $("#no-results").empty();
                        }        
                response(results.slice(0, 10));
                },          
            focus: function (event, ui) {
                $(event.target).val(ui.item.label);
                return false;
            },
            select: function (event, ui) {
                $(event.target).val(ui.item.label);
                window.location = ui.item.value;
                return false;
            }               
                });
    });
//]]>  
  </script>

回答1:


Instead of using $("#no-results").text("<a href=\"/\">No results found!</a>") try $("#no-results").html('<a href="">No results found!</a>'). Although why you want an anchor tag with no link confuses me.



来源:https://stackoverflow.com/questions/8863628/no-matches-message-for-jquery-ui-autocomplete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!