I'M老顽童 2011-05-07 10:53 采纳率: 0%
浏览 340
已采纳

如何用Ajax实现类似百度搜索效果

实现的效果是 我们可以利用 上 下 键 可以选择 从数据库当中循环出来的数据 并且 搜索栏里面的 input 当中的值会 改变成当前 选择的元素的值 只要告诉我如何 利用 上 下 键可以选择元素就可以了 谢谢^_^

  • 写回答

9条回答 默认 最新

  • suziwen 2011-05-07 13:29
    关注

    [url]http://jqueryui.com/demos/autocomplete/#remote-jsonp[/url]

    下面有VIEW SOURCE
    [code="java"]

    <style>
    .ui-autocomplete-loading { background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat; }
    #city { width: 25em; }
    </style>
    <script>
    $(function() {
        function log( message ) {
            $( "<div/>" ).text( message ).prependTo( "#log" );
            $( "#log" ).attr( "scrollTop", 0 );
        }
    
        $( "#city" ).autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: "http://ws.geonames.org/searchJSON",
                    dataType: "jsonp",
                    data: {
                        featureClass: "P",
                        style: "full",
                        maxRows: 12,
                        name_startsWith: request.term
                    },
                    success: function( data ) {
                        response( $.map( data.geonames, function( item ) {
                            return {
                                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                                value: item.name
                            }
                        }));
                    }
                });
            },
            minLength: 2,
            select: function( event, ui ) {
                log( ui.item ?
                    "Selected: " + ui.item.label :
                    "Nothing selected, input was " + this.value);
            },
            open: function() {
                $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
            },
            close: function() {
                $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
            }
        });
    });
    </script>
    
    Your city: Powered by geonames.org
    Result:

    <!-- End demo -->

    The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are cities, displayed when at least two characters are entered into the field.

    In this case, the datasource is the geonames.org webservice. While only the city name itself ends up in the input after selecting an element, more info is displayed in the suggestions to help find the right entry. That data is also available in callbacks, as illustrated by the Result area below the input.

    <!-- End demo-description -->[/code]
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?