【问题标题】:How to hide blank array and just leave array's value/name?如何隐藏空白数组并只保留数组的值/名称?
【发布时间】:2015-08-10 13:51:44
【问题描述】:

我正在使用 jquery 自动完成插件。如何隐藏空白数组并只保留数组的值/名称?例如

HTML:

 <script type="text/javascript">

    $(function() {

        $("#ac5").autocomplete('search.php', {
            minChars: 1,

        });

       });

        </script>
    <form>
            <input type="text" id="ac5">
        </form>

php:

/*
 * Load sample data
 */

$dir    = '../image/imagefiles/';
$images = scandir($dir);




/*
 * Results array
 */
$results = $images;

/*
 * Autocomplete formatter
 */
function autocomplete_format($results) {
    foreach ($results as $result) { 
        echo $result;
    }
}

/*
 * Output format
 */
$output = 'autocomplete';
if (isset($_GET['output'])) {
    $output = strtolower($_GET['output']);
}

/*
 * Output results
 */
if ($output === 'json') {
    echo json_encode($results);
} else {
    echo autocomplete_format($results);
}

自动完成的输出:...Dj_Etrom_Remix1.jpgDj_Etrom_Remix2.jpgDj_Etrom_Remix3.jpg 它在同一行显示所有文件名,但我想这样做Dj Etrom Remix.jpg

【问题讨论】:

  • 你的行$data = array(print_r($images) ); 很奇怪...... print_r 是一个字符串,所以我不相信它是一个数组......你为什么不直接使用$images 而不是@987654327 @ ?
  • 删除 $data = array( print_r($images) ); 并将 $results = $data; 更改为 $results = $images; 行,看看会发生什么
  • 工作,但它在同一行显示值。 @病毒
  • “在同一行”是什么意思?您可以在问题中添加“print_r($images)”的输出,以查看数据的结构吗?
  • 如果类型不是json,也请提及您想要的输出。

标签: javascript php jquery arrays autocomplete


【解决方案1】:

问题在于your autocomplete_format 函数,即echo 数组的每个元素...

您可以在其中做任何您想做的事情。
如果要将所有文件都放在新行中,请使用echo $result."\n";
如果您希望在 html 页面中使用,请使用 echo $result."&lt;br/&gt;";,
如果你想用逗号分隔它们,请使用echo $result.", ";...

但是使用 jQuery 自动完成,你应该将它作为 json 数组发送...

【讨论】:

  • 非常感谢您与echo $result."\n"; 合作。
猜你喜欢
  • 2013-04-25
  • 2021-06-27
  • 2017-05-03
  • 2018-03-19
  • 2014-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多