让我尝试用我尝试过的代码片段再次询问这个问题
我正在尝试格式化 Jquery 自动完成以包含每个数据源的标题并突出显示该术语。我正在使用 Codeigniter,并认为最简单的方法是在将其发回之前对其进行格式化:
JS:
$( ".auto-search" ).autocomplete({
source: '/json/autocomplete_search',
minLength: 2,
});
PHP(代码点火器)
public function autocomplete_search()
{
$term = $this->input->get('term');
//load model and get results
$this->load->model("mymodel");
$results1= $this->mymodel->search_one($term);
$results2= $this->mymodel->search_two($term);
//Start JSON string
$json='[';
//first source
if ($result1->num_rows() > 0)
{
$json .= '{"value":"<h3>Heading One<h3>"}';
foreach ($results1->result_array() as $r1)
{
$result = str_replace($term,"<strong>".$term."</strong>",$r1['title']);
$json .= ',{"value":"'.$result.'"}';
}
}
//second source
if ($result2->num_rows() > 0)
{
if ($result1->num_rows() > 0){$json .= ',';}
$json .= '{"value":"<h3>Heading Two<h3>"}';
foreach ($results2->result_array() as $r2)
{
$result = str_replace($term,"<strong>".$term."</strong>",$r2['location']);
$json .= ',{"value":"'.$result.'"}';
}
}
//Close JSON string
$json .= ']';
echo $json;
}`
不幸的是,我没有得到格式化的输出,相反,它实际上在输出中添加了 <h1> 和 <strong> 这两个词。这是示例输出: