doujia6433 2012-05-14 03:41
浏览 46
已采纳

bootstrap typeahead获取结果数据源结果出现?

i am not getting any results when i search.

<input type="text" class="span1" id="tag_field" data-items='4' data-provide="typeahead" data-source='[<?php echo json_encode($groups); ?>]' >
<?php echo json_encode($groups); ?>

when i echo out json_encond($groups) it appears in this format

{"35":"biology","37":"economist","33":"programmers"} 

if i type in the data source using this format i do get results.

 data-source='["Alabama","Alaska","Arizona"]'>
  • 写回答

1条回答 默认 最新

  • dpli36193 2012-05-14 03:48
    关注

    I think the Typeahead plugin is expecting an Array of Strings as the data-source. Your json_encode is creating an Object and you're just wrapping it in an array when you echo it.

    You want something like this:

    <?php
    $groups = array("biology", "economist", "programmers");
    ?>
    
    <input type="text" class="span1" id="tag_field" data-items='4' data-provide="typeahead" data-source='<?php echo json_encode($groups); ?>'>
    

    You can use the array_values() function in PHP to ensure your $groups is a basic numerically indexed array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?