我正在使用jQuery (1.12.4)
和Select2 (4.0.3)
从数据库中选择多个值,但我遇到了以下错误。
TypeError:b未定义
JS代码
$('#country').select2(
{
placeholder: '--- Select ---',
multiple: true,
ajax:
{
url: '<?=site_url()?>/countries',
dataType: 'json',
type: 'POST',
delay: 1000,
processResults: function (data)
{
console.log(data);
var myResults = [];
$.each(data, function (index, item)
{
myResults.push(
{
'id': item.id,
'text': item.text
});
});
return
{
results: myResults
};
},
cache: true
}
});
JSON响应
[{"id":"US","text":"United States"},{"id":"RE","text":"Reunion"},{"id":"TZ","text":"Tanzania, United Republic of"},{"id":"TN","text":"Tunisia"},{"id":"AE","text":"United Arab Emirates"},{"id":"GB","text":"United Kingdom"},{"id":"UM","text":"United States minor outlying islands"}]
HTML非常简单
<select id="country"></select>
PHP代码
$this->db->like('name', $this->input->post("q"));
$query = $this->db->select('code AS id, name as text')
->limit(20)
->get("countries");
$records = $query->result();
echo json_encode($records);