如何将隐藏ID与自动填充jquery

时间:2015-12-30 03:47:49

标签: javascript jquery codeigniter autocomplete

我希望在从自动完成中获取值时添加隐藏ID以添加到数据库但现在当我从自动完成获取值并添加到数据库时,它的添加值是标签名称而不是数据的ID

<input type='text' id='id' name='id' class='id_line' placeholder='Item Code'/>

这是我的jquery代码

$("#add_line").on('click',function(){

            $('.tbl_add_line').append(data);
            $('.id_line').autocomplete({
                source: function (request, response){
                    $.ajax({
                        url : "<?php echo site_url('warehouse/get_id');?>",
                        dataType: "jsonp",
                        data : {
                            q: request.term
                        },
                        success: function( data ) {
                            response(data);
                        }
                        });
                },
            });
            i++;
        });

这是使数据显示在自动完成中的功能

function get_id(){

         $result = "";
         if(isset($_GET['q'])){ 
            $code = $_GET['q'];
            $this->db->like("item_code",$code)->select('*'); 
            $this->db->order_by("item_code");
            $result = $this->db->from("tbl_item_code")->get()->result();
         }
         $arr = array();
         foreach($result as $key){
            $arr[] = array( 
                'id' => $key->id,
                'value' => $key->id,
                'label' => $key->item_code."(".$key->id.")",

            );
         }
         echo $_GET['callback']."(".json_encode($arr).")";


}

1 个答案:

答案 0 :(得分:1)

您可以使用_renderItem方法将custom attributes添加到已创建的元素。

试试这个:

&#13;
&#13;
$(function() {
  var projects = [{
    value: "jquery",
    label: "jQuery",
    desc: "the write less, do more, JavaScript library",
    icon: "jquery_32x32.png"
  }, {
    value: "jquery-ui",
    label: "jQuery UI",
    desc: "the official user interface library for jQuery",
    icon: "jqueryui_32x32.png"
  }, {
    value: "sizzlejs",
    label: "Sizzle JS",
    desc: "a pure-JavaScript CSS selector engine",
    icon: "sizzlejs_32x32.png"
  }];

  $("#project").autocomplete({
      minLength: 0,
      source: projects,
      focus: function(event, ui) {
        $("#project").val(ui.item.label);
        return false;
      },
      select: function(event, ui) {
        $("#project").val(ui.item.label);
        $("#project-id").val(ui.item.value);
        $("#project-description").html(ui.item.desc);
        return false;
      }
    })
    .autocomplete("instance")._renderItem = function(ul, item) {
      return $("<li>")
        .append("<a>" + item.label + "<br>" + item.desc + "</a>")
        .appendTo(ul);
    };
});
&#13;
    #project-label {
      display: block;
      font-weight: bold;
      margin-bottom: 1em;
    }
    #project-icon {
      float: left;
      height: 32px;
      width: 32px;
    }
    #project-description {
      margin: 0;
      p
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />

<div id="project-label">
  Select a project (type "j" for a start):</div>
<input id="project">
<input type="text" id="project-id" disabled>
<p id="project-description"></p>
&#13;
&#13;
&#13;

Fiddle here