我想在我的javascript中将一些数据从mysql中导入我的autosuggest。 问题是wird:程序运行正常,但是当你在文本框中写一个值时,会弹出“空”的提示..
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'])
;只有数组中的最后一个值出现在我的autosuggest中。然后它工作正常!但是当我把它改成阵列时。它会起作用,但只会显示空的建议。
}
使用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>
我做错了什么?我真的希望你理解这个问题。如果没有随意问:)
答案 0 :(得分:0)
根据文件:http://api.jqueryui.com/autocomplete/#option-source
有两种支持的格式:
字符串数组:[“Choice1”,“Choice2”]
具有label和value属性的对象数组:[{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); ?>;