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"]'>

图片转代码服务由CSDN问答提供 功能建议

我搜索时没有得到任何结果。

   &lt; input type =“text”class =“span1”id =“tag_field”data-items ='4'ata-provide =“typeahead”data-source ='[&lt;?php echo json_encode($ groups);  ?&gt;]'&gt; 
&lt;?php echo json_encode($ groups);  ?&gt; 
   
 
 

当我回显出json_encond($ groups)时,它会以这种格式显示

  {“  35“:”biology“,”37“:”经济学家“,”33“:”程序员“} 
   
 
 

如果我使用这种格式输入数据源 我确实得到了结果。

  data-source ='[“Alabama”,“Alaska”,“Arizona”]'&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题