【问题标题】:jQuery UI autocomplete in codeIgniter with sql, php使用 sql、php 在 codeIgniter 中的 jQuery UI 自动完成
【发布时间】:2015-10-27 07:12:38
【问题描述】:

我正在尝试使用 jquery 创建文本框自动完成功能。但是当我在调试器中检查它时出现了问题,它显示了每个值,但它没有显示在视图页面的文本框下拉列表中。我是新手,所有帮助将不胜感激。

这是我的视图代码。

<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>

<script type="text/javascript">
    $(function(){
        $("#location").autocomplete({
            source: 'set_location'
        });     
    });
</script>
<form action= "#">
    <table>
        <tr>
            <td><label for='location'>Location</label></td>
            <td><input id="location" type="text" size="50" placeholder="Enter a location" name= "location"> </td>
        </tr>           
    </table>
</form>

这是我的控制器代码

if(isset($_GET['term'])){
    $location = strtolower($_GET['term']);      
    $query = $this->venues_model->set_location($location);          
    echo $query;
}

这是我的型号代码

$this->db->select('*');
$this->db->like('location', $location);
$query = $this->db->get('venue_details');

if(count($query->result_array()) > 0){ 
    foreach ($query->result_array() as $row){
        $row_set[] = htmlentities(stripslashes($row['location']));
    }
    echo json_encode($row_set);
}

【问题讨论】:

  • 控制台有错误吗??
  • 不,它什么也没显示。

标签: php jquery sql codeigniter


【解决方案1】:

试试这个,

if(isset($_GET['term'])){
$location = strtolower($_GET['term']);      
echo $this->venues_model->set_location($location);          
}

在模型中

function set_location() {
    $row_set = array();
    $this->db->select('*');
    $this->db->like('location', $location);
    $query = $this->db->get('venue_details');

    if (count($query->result_array()) > 0) {
        foreach ($query->result_array() as $row) {
            $row_set[] = htmlentities(stripslashes($row['location']));
        }

    }

    return json_encode($row_set);
}

还有这个,

source: '<?= base_url("controller/set_location") ?>'

【讨论】:

  • 检查我是否改变了我的模型
  • 在控制台打印什么??
  • 是的,每次按键后都会更新并显示值。
  • 你可以添加那个截图吗??
  • 在此,只应出现 json 的回声。没有 var_dump,没有任何其他回声,也没有加载视图
【解决方案2】:

你必须从你的模型文件中return,从你的控制器中echo

型号

$this->db->select('*');
$this->db->like('location', $location);
$query = $this->db->get('venue_details');

if(count($query->result_array()) > 0){ 
    foreach ($query->result_array() as $row){
        $row_set[] = htmlentities(stripslashes($row['location']));
    }
    return json_encode($row_set);// return from model
}

控制器

if(isset($_GET['term'])){
    $location = strtolower($_GET['term']);      
   echo  $query = $this->venues_model->set_location($location);          
}

然后改变你的路径

source: '<?php echo base_url();?>index.php/controller/function'

【讨论】:

  • 您需要调用 ajax link 从控制器获取数据
  • 我正在关注这个link
  • 你需要检查你的路径我可能是来源:'
  • 这里的 index.php 是什么
  • 检查没有 index.php &lt;?php echo base_url();?&gt;controller/function
【解决方案3】:

只需将ui-widget 添加到您的 div 中。确保引用 jquery ui

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-05
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    相关资源
    最近更新 更多