views.php
<div class="input-group">
<input class="form-control" id="nomor_cari" maxlength="16" placeholder="No. CM / No. KTP">
脚本自动完成
$('#nomor_cari').autocomplete({
source: get_no,
dataType: 'JSON'
});
函数get_no
<?php
function get_no($q)
{
$this->db->select('NO_MEDREC');
$this->db->like('NO_MEDREC', $q);
$query = $this->db->get('PASIEN_IRJ');
if($query->num_rows > 0)
{
foreach ($query->result_array() as $row)
{
$row_set[] = htmlentities(ucfirst($row['NO_MEDREC']));
}
$this->output
->set_content_type('application/json')
->set_output(json_encode($row_set));
}
echo $query->row();
}?>
但自动完成没有显示任何内容。
如果我使用局部变量,自动完成的工作方式应该是这样的。
<script> var availableNumber = [
"0000000002",
"0000000020",
"0000000200",
"0000002000"
];
/<script>
并将自动完成更改为
$('#nomor_cari').autocomplete({
source: availableNumber,
dataType: 'JSON'
});
我错过了什么?(我确信.js是加载的,因为当使用本地var时,它的工作方式就像魅力一样)
以防万一,这是我的自动加载
$autoload['libraries'] = array('database', 'email', 'session');
$autoload['helper'] = array('url', 'template', 'message', 'misc');
我认为问题出在"源代码"上。请尝试使用类似的json
$('#nomor_cari').autocomplete({
source: function (request, response) {
$.getJSON("ful_url/get_no?term=" + request.term, function (data) {
response($.map(data.dealers, function (value, key) {
return {
label: value,
value: key
};
}));
});
},
});