【问题标题】:fetch id and store it in hidden text field on autocomplete select using codeigniter使用 codeigniter 获取 id 并将其存储在自动完成选择的隐藏文本字段中
【发布时间】:2013-10-18 06:11:51
【问题描述】:

这是我的视图代码

<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;
}
}

jquery 代码

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

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

【问题讨论】:

    标签: php codeigniter jquery jquery-autocomplete


    【解决方案1】:
     $(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
      }
     });  
    });
    

    【讨论】:

    • 您如何在此处获取数据? source 在您的autocomplete 中在哪里?
    • 当用户单击我需要在数据库中保存相应 ID 的名称时,我需要向用户显示名称。这是我从 db 获取名称和 id 的编码(即参见控制器,我使用 echo 打印名称和 id)该名称和 id 不够,我需要在前端将其分开。我的问题是我在控制器中回显的内容单独显示在显示器中,这意味着我不能分别使用 id 和 name 都出现在同一个文本框中,因此我无法将 id 保存在 db 中。
    猜你喜欢
    • 1970-01-01
    • 2018-05-03
    • 2019-09-30
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    相关资源
    最近更新 更多