0

我正在尝试从 mysql 中获取一些数据到我的 javascript 中的自动提示中。问题很奇怪:程序运行正常,但是当你在文本框中输入一个值时,会弹出“空”的提示。

PHP:

  $sqldropdown = $this->EE->db->query("SELECT emd.m_field_id_8 FROM transactions as t
  left join exp_members as em on (t.cardid-10000000 = em.member_id) 
  left JOIN exp_member_data emd on em.member_id = emd.member_id group by emd.m_field_id_8 ASC");

  foreach ($sqldropdown->result_array() as $filterofresults) 
  {
  $samletdropdown[]=$filterofresults;
  }

  foreach ($samletdropdown as $key => $value) 
  {
  $victims[]= array($value['m_field_id_8']);

如果将此行更改为: $victims= array($value['m_field_id_8']); 只有数组中的最后一个值出现在我的自动建议中。然后它工作正常!但是当我将其更改为数组时。它会起作用,但只会出现空的建议。}

Javascript:

<meta charset="utf-8" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {

var availableTags = <?php echo json_encode($victims); ?>;

$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>

html:

  </div>  

  <div class="span2 well " style="height:100px;">
  <p>
  <label for="tags">Search box: </label>

  <input type="text" id="tags" />
  </p>  

  </div>  

我究竟做错了什么?我真的希望你能理解这个问题。如果不随意问:)

4

1 回答 1

0

根据文档:http ://api.jqueryui.com/autocomplete/#option-source

有两种支持的格式:

字符串数组:[ "Choice1", "Choice2" ]

具有标签和值属性的对象数组: [ { label: "Choice1", value: "value1" }, ... ]

因此,您需要像这样创建数组:

  foreach ($samletdropdown as $key => $value) 
  {
      $data = array();
      $data['label'] = 'Label for data : '.key;
      $data['value'] = $value['m_field_id_8'];

      $victims[] = $data;

      //Or directly but without redefining a new array
      $victims[] = $value['m_field_id_8'];
  }

在像你已经做的那样使用之前:

var availableTags = <?php echo json_encode($victims); ?>;
于 2013-03-17T14:16:39.493 回答