【问题标题】:Auto complete not showing result jquery自动完成不显示结果jquery
【发布时间】:2012-11-08 19:27:08
【问题描述】:

我正在尝试使用 jqueryui 创建一个自动完成。我正在回显来自远程文件 search.php 的数据库结果。它在火灾错误的响应中显示了正确的单词,但建议列表根本没有显示在我的 html 页面中。 有人可以帮帮我吗?

我在 jqueryui.com 中使用 multipile ,remote demo 的代码

我的 php 代码

<?php include("connection.php");
 $q=mysql_real_escape_string($_GET['term']); 
$x="select fieldname from tablename where fieldname like '$q%'"; 
$query=mysql_query($x); 
while($row=mysql_fetch_array($query)) { echo $row['fieldname']."\n"; } ?>
========================================================================

    ------------------------------------------------------------------------

    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
        <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
        <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
        <link rel="stylesheet" href="/resources/demos/style.css" />
        <style>
        .ui-autocomplete-loading {
            background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat;
        }
        </style>
        <script>
        $(function() {
            function split( val ) {
                return val.split( /,\s*/ );
            }
            function extractLast( term ) {
                return split( term ).pop();
            }

            $( "#birds" )
                // don't navigate away from the field on tab when selecting an item
                .bind( "keydown", function( event ) {
                    if ( event.keyCode === $.ui.keyCode.TAB &&
                            $( this ).data( "autocomplete" ).menu.active ) {
                        event.preventDefault();
                    }
                })
                .autocomplete({
                    source: function( request, response ) {
                        $.getJSON( "search.php", {
                            term: extractLast( request.term )
                        }, response );
                    },
                    search: function() {
                        // custom minLength
                        var term = extractLast( this.value );
                        if ( term.length < 2 ) {
                            return false;
                        }
                    },
                    focus: function() {
                        // prevent value inserted on focus
                        return false;
                    },
                    select: function( event, ui ) {
                        var terms = split( this.value );
                        // remove the current input
                        terms.pop();
                        // add the selected item
                        terms.push( ui.item.value );
                        // add placeholder to get the comma-and-space at the end
                        terms.push( "" );
                        this.value = terms.join( ", " );
                        return false;
                    }
                });
        });
        </script>
    </head>
    <body>

    <div class="ui-widget">
        <label for="birds">Birds: </label>
        <input id="birds" size="50" />
    </div>

【问题讨论】:

  • 如果我们看不到您的代码,谁能帮助您?我们是否应该猜出问题所在?
  • 您需要在此处放置一些代码,这样人们就不必猜测出了什么问题。你没有提供任何有用的信息来帮助你。

标签: php jquery firebug


【解决方案1】:

如果您的远程文件位于不同的域中,您必须使用 JSONP。 JSON 不支持跨域数据传输。

阅读更多关于Same Origin policy

【讨论】:

  • 我在 localhost 工作,我可以在 firebug 的响应选项卡中看到响应
  • 我对 JSON 不太了解,你能回答我为什么响应显示在萤火虫响应下,为什么它没有出现在 html 页面中?
  • 而不是 while($row=mysql_fetch_array($query)) { echo $row['fieldname']."\n"; } 将 $row['fieldname'] 存储在变量中,例如 $var.=$row['fieldname'].",";并通过 echo $var;外循环。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-09
  • 2011-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多