我有这个ac.php文件和这个result.php文件我想在ac.php文件中使用JQuery UI中的自动完成,但我希望它从result.php中检索数据作为源。 我只是不知道该怎么做。 我试过这种方式,但它不起作用
ac.php
<html> <head> <link type="text/css" href="jqui/css/ui-lightness/jquery-ui-1.8.11.custom.css" rel="stylesheet" /> <script type="text/javascript" src="jqui/js/jquery-1.5.1.min.js"></script> <script type="text/javascript" src="jqui/js/jquery-ui-1.8.11.custom.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#tags").autocomplete("result.php"); }); </script> </head> <body> <div class="ui-widget"> <label for="tags">Tags: </label> <input id="tags"> </div> </body> </html>这是result.php
<?php $arrResults = array('option 1', 'option 2', 'option 3'); // Print them out, one per line echo implode("\n", $arrResults); ?>I have this ac.php file and this result.php file I want to use the autocomplete in JQuery UI in the ac.php file, but I want it to retrieve the data from result.php as a source. I just dont know how to do it. I tried this way, but its not working
ac.php
<html> <head> <link type="text/css" href="jqui/css/ui-lightness/jquery-ui-1.8.11.custom.css" rel="stylesheet" /> <script type="text/javascript" src="jqui/js/jquery-1.5.1.min.js"></script> <script type="text/javascript" src="jqui/js/jquery-ui-1.8.11.custom.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#tags").autocomplete("result.php"); }); </script> </head> <body> <div class="ui-widget"> <label for="tags">Tags: </label> <input id="tags"> </div> </body> </html>this is result.php
<?php $arrResults = array('option 1', 'option 2', 'option 3'); // Print them out, one per line echo implode("\n", $arrResults); ?>最满意答案
引用自动完成的文档 :
自动完成插件期望该字符串指向将返回JSON数据的URL资源。
因此,您应该使用以下内容代替您的代码:
<?php $arrResults = array('option 1', 'option 2', 'option 3'); echo json_encode($arrResults); ?>json_encode()函数将返回与您的数组对应的JSON字符串。
Quoting the documentation of Autocomplete :
the Autocomplete plugin expects that string to point to a URL resource that will return JSON data.
So, instead of your code, you should use something like this :
<?php $arrResults = array('option 1', 'option 2', 'option 3'); echo json_encode($arrResults); ?>The json_encode() function will return the JSON string that corresponds to your array.
更多推荐
发布评论