用jQuery自动生成前端表单域

时间:2016-01-09 作者:Christy

我正在尝试从前端字段自动生成自定义分类法中的术语。然而,当我使用下面的代码并尝试键入字母时,出现的下拉菜单是一堆黑框(下面的屏幕截图)。任何帮助都将不胜感激。如果你需要更多信息,请告诉我。谢谢

<?php 
$args = array(
    \'orderby\'                => \'name\',
    \'order\'                  => \'ASC\',
    \'hide_empty\'             => false,
    \'include\'                => array(),
    \'exclude\'                => array(),
    \'exclude_tree\'           => array(),
    \'number\'                 => \'\',
    \'offset\'                 => \'\',
    \'fields\'                 => \'all\',
    \'name\'                   => \'\',
    \'slug\'                   => \'\',
    \'hierarchical\'           => true,
    \'search\'                 => \'\',
    \'name__like\'             => \'\',
    \'description__like\'      => \'\',
    \'pad_counts\'             => false,
    \'get\'                    => \'\',
    \'parent\'                 => \'0\',
    \'childless\'              => false,
    \'cache_domain\'           => \'core\',
    \'update_term_meta_cache\' => true,
    \'meta_query\'             => \'\'
);
$phpArray = get_terms( \'listing_taxonomy\', $args );
?>
<script>
    $(function() {
    var availableTags = <?php echo json_encode($phpArray); ?>;
    $( "#tags" ).autocomplete({
      source: availableTags
    });
    });
</script>

enter image description here

呼叫控制台。日志(availableTags)提供以下信息:enter image description here

1 个回复
最合适的回答,由SO网友:WPTC-Troop 整理而成

您好,自动完成预期源为array, URL string and function return. 即使你付出JSON 那么它应该label and value. 此处不存在,因为您只是输出get_terms 具有all 字段。

所以我建议只返回类别的名称。更改字段allnames 将很好地工作。

<?php 
$args = array(
    \'orderby\'                => \'name\',
    \'order\'                  => \'ASC\',
    \'hide_empty\'             => false,
    \'include\'                => array(),
    \'exclude\'                => array(),
    \'exclude_tree\'           => array(),
    \'number\'                 => \'\',
    \'offset\'                 => \'\',
    \'fields\'                 => \'names\',
    \'name\'                   => \'\',
    \'slug\'                   => \'\',
    \'hierarchical\'           => true,
    \'search\'                 => \'\',
    \'name__like\'             => \'\',
    \'description__like\'      => \'\',
    \'pad_counts\'             => false,
    \'get\'                    => \'\',
    \'parent\'                 => \'0\',
    \'childless\'              => false,
    \'cache_domain\'           => \'core\',
    \'update_term_meta_cache\' => true,
    \'meta_query\'             => \'\'
);
$phpArray = get_terms( \'listing_taxonomy\', $args );
?>
<script>
    $(function() {
    var availableTags = <?php echo json_encode($phpArray); ?>;
    $( "#tags" ).autocomplete({
      source: availableTags
    });
    });
</script>