2013-04-01 57 views
0

我是JavaScript新手,我試圖按照this教程。codeigniter jquery的自動完成功能無法正常工作,但沒有錯誤

我有一個文本框(輸入),我想通過從MySQL加載一些數據來使用jQuery的自動完成功能。

這是我的控制器代碼:

public function autocomplete() { 
    if($this->input->post('txt_nama')) 
     $this->absensi_m->get_umat($this->input->post('txt_nama')); 

    /*if (isset($_GET['term'])){ 
     $q = strtolower($_GET['term']); 
     $this->absensi_m->get_umat($q); 
    }*/ 
} 

這是我的模型:

public function get_umat($word) { 
    $this->db->select('nama', 'tanggal_lahir'); 
    $this->db->like('nama', $word); 
    $query = $this->db->get('msumat'); 

    if($query->num_rows() > 0) 
    { 
     foreach($query->result_array() as $row) 
     { 
      $new_row['label'] = htmlentities(stripslashes($row['nama'])); 
      $new_row['value'] = htmlentities(stripslashes($row['tanggal_lahir'])); 
      //build array 
      $row_set[] = $new_row; 
     } 
     echo json_encode($row_set); 
    } 
} 

這是我的javascript:

<script type="text/javascript"> 
     $(function(){ 
      $("#txt_nama").autocomplete({ 
       source: "autocomplete" 
      }); 
     }); 
    </script> 

我試圖通過檢查的JavaScript firefox的螢火蟲和GC的開發者工具,這就是我得到的:

<input type="text" id="txt_nama" name="txt_nama" class="ui-autocomplete-input" autocomplete="off"> 

注意,自動完成關閉。我想這就是問題所在,所以我試圖將此代碼添加到打開它:

$(document).ready(function() { 
      $("#txt_nama").attr("autocomplete", "on"); 
     }); 

自動完成元件接通當我添加此代碼,但是自動完成仍然工作。

我還試圖用回聲,但我的回聲沒有工作:

if($query->num_rows() > 0) 
    { 
     echo num_rows(); 
     echo 'a'; 

     foreach($query->result_array() as $row) 
     { 
      $new_row['label'] = htmlentities(stripslashes($row['nama'])); 
      $new_row['value'] = htmlentities(stripslashes($row['tanggal_lahir'])); 
      //build array 
      $row_set[] = $new_row; 
     } 
     echo json_encode($row_set); 
     //return row_set; 
    } 

我缺少什麼?

注: 我只是想知道Routes,它是與此相關的錯誤,因爲通常情況下,人們在source:(JavaScript)的使用controller/method,但我不能這樣做,因爲生成的路線將有雙控制器( index.php/absensi/absensi/autocomplete),所以我除去controller,只是使用的方法(source: "absensi"

回答

0

這就是答案:

  1. 不要使用回聲,它會打破jQuery。

  2. 使用if (isset($_GET['term'])),而不是$this->input->post()

  3. 在jQuery的

    使用source: "<?php echo site_url('absensi/autocomplete'); ?>"

1

模型

public function get_umat($word) { 
    $this->db->select('nama', 'tanggal_lahir'); 
    $this->db->like('nama', $word); 
    $query = $this->db->get('msumat'); 

    if($query->num_rows() > 0) 
    { 
     foreach($query->result_array() as $row) 
     { 
      $new_row['label'] = htmlentities(stripslashes($row['nama'])); 
      $new_row['value'] = htmlentities(stripslashes($row['tanggal_lahir'])); 
      //build array 
      $row_set[] = $new_row; 
     } 
     return $row_set; 
    } 
} 

控制器:

public function autocomplete() { 
    if($this->input->post('txt_nama')) 
     echo json_encode($this->absensi_m->get_umat($this->input->post('txt_nama'))); 

} 

你應該放在autocomplete()echo json_encode()但不是在模型..

+0

感謝您的幫助:D看來你的控制器代碼給出一個錯誤,我嘗試現在來解決它:d –

2

我相信你正在使用source選項不正確。從docs

當使用字符串時,自動填充插件希望該字符串指向將返回JSON數據的URL資源。

如果你的意思是有source指向您autocomplete功能,你需要給你的控制器的名字一樣,他們對你鏈接的演示:

source: "birds/get_birds" // path to the get_birds method 
+1

同樣使用CI的site_url方法可能會有所幫助。 –

+0

@MihaiIonescu感謝您的幫助。我嘗試使用源代碼:「<?php echo site_url();?>/absensi/autocomplete」,它會生成相同的路線,就像我使用「自動完成」一樣,但它仍然不工作:D –

+0

感謝您的幫助:DI也曾經相信,但是當我使用像「absensi/autocomplete」(absensi是我的控制器和自動完成是一種方法),它會給我錯誤404(因爲生成的路線是「index.php/absensi/absensi /自動完成「 - 注意,absensi被調用兩次,這是404錯誤),但如果我只是使用」自動完成「在我的螢火蟲/開發控制檯沒有錯誤:D –

相關問題