简体   繁体   English

自动完成功能在ci中无法正常工作

[英]Autocomplete does't work properly in ci

controller: welcome.php 控制器:welcome.php

public function lookup()
{
    $keyword = trim($_GET['term']);
    $data['response'] = 'false';
    $query = $this->Main_tutorial->lookup($keyword);
    print_r($query);
    if( ! empty($query) )  
    {  
        $data['response'] = 'true';
        $data['message'] = array();  
        $data['auto_com'] = array();  
        foreach( $query as $row )  
        {  
            $data['message'][] = array('tutorial_name' => $row['tutorial_name']);
            $data['auto_com'][] = $row['tutorial_name'];   
        }  
    }  
    if('IS_AJAX')  
    {  
        echo json_encode($data['auto_com']);
    }  
    else 
    {  
        $this->load->view('index',$data);
    }
}

view: index.php 查看:index.php

<html>
<head>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>resource/css/jquery-ui.css">
    <link rel="stylesheet" href="http://static.jquery.com/ui/css/demo-docs-theme/ui.theme.css" type="text/css" media="all" /> 
</head>
<body>
    <input type="text" class="search-bg__text" name="tutorial_name" id="tutorial_name">
    <script src="<?php echo base_url(); ?>resource/js/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#tutorial_name").autocomplete({
                source:"<?php echo base_url(); ?>welcome/lookup",
                minLength:1,
                select:function(b,c)
                {
                    var a=c.item.value;
                    a=encodeURIComponent(a);
                    location.href=a+"-tutorial";
                    return false
                }
            });
        });
    </script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js" type="text/javascript"></script>
</body>
</html>

model: Main_tutorial.php 模型:Main_tutorial.php

public function lookup($keyword)
{
    $this->db->select('tutorial_name')->from('tutorial'); 
    $this->db->like('tutorial_name',$keyword,'after');  
    $query = $this->db->get();
    $result = $query->result_array();     
    return $result;
}                 

In this code I want to create an autocomplete suggestion box using codeigniter. 在这段代码中,我想使用codeigniter创建一个自动完成建议框。 Now what happen when I write something inside the tutorial_name input field and inspect the element then select network and click on link it show me the output but can't see on display I don't know why ?. 现在,当我在tutorial_name输入字段中编写内容并检查元素,然后选择network并单击链接时会发生什么,该链接向我显示输出,但看不到显示,我不知道为什么? So how can I fix this issue ?Please help me. 那么如何解决此问题?请帮助我。

Thank You 谢谢

In autocomplete() you need output as json format. autocomplete()您需要输出为json格式。 Here you are printing json data with echo json_encode($data['auto_com']); 在这里,您将使用echo json_encode($data['auto_com']);打印JSON数据echo json_encode($data['auto_com']); But before it you are also displaying data print_r($query); 但在此之前,您还显示了数据print_r($query); . Which create invalid json output. 其中创建无效的json输出。 so you have to remove it. 因此您必须将其删除。 Also note that your if('IS_AJAX') will always become true 另请注意,您的if('IS_AJAX')将始终为true

public function lookup()
{
    $keyword = trim($_GET['term']);
    $data['response'] = 'false';
    $query = $this->Main_tutorial->lookup($keyword);
    if( ! empty($query) )  
    {  
        $data['response'] = 'true';
        $data['message'] = array();  
        $data['auto_com'] = array();  
        foreach( $query as $row )  
        {  
            $data['message'][] = array('tutorial_name' => $row['tutorial_name']);
            $data['auto_com'][] = $row['tutorial_name'];   
        }  
    }  
    if('IS_AJAX')  //This condition always become true
    {  
        echo json_encode($data['auto_com']);
    }  
    else 
    {  
        $this->load->view('index',$data);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM