获取 ID 并将其存储在使用代码点火器自动完成选择时的隐藏文本字段中


fetch id and store it in hidden text field on autocomplete select using codeigniter

这是我的视图代码

<input type="text" name="chname" id="chrname" placeholder="church name" />

这是我的控制器代码

function auto_comp(){
   $q = strtolower($_GET["q"]);
    if(!$q) return;
    $res=$this->churchmodel->auto_compt($q);
    if($res){
      foreach($res as $row){
        //echo row->chid;echo "<input type='"text'" value='"".$row->chid."'" />";echo row->churchname."'n"; 
      }
    }
  }

这是我的模型代码

function auto_compt($name){
$val="%".$name."%";
$query=$this->db->query("select distinct name as churchname, id as chid from church where name like '$val'");
if($query->num_rows>0){
    return $query->result();
}else{
    return false;
}
}

j查询代码

 $().ready(function() {
  $("#chrname").autocomplete("localhost/deeps/genting/index.php/church/auto_comp", {
     width: 260,
     matchContains: true,
     selectFirst: false
  });  
 });

在这里它工作得很好,但我需要在特定的选择上获取相应的 ID,以便我可以轻松存储所选值的 ID。 请提前帮忙谢谢

 $(document).ready(function() {
  $("#chrname").autocomplete("localhost/deeps/genting/index.php/church/auto_comp", {
  width: 260,
  matchContains: true,
  selectFirst: false,
  select: function (event) {
   alert(event.target.id)   // fetch your id here
  }
 });  
});