dsgdfh302506 2014-12-08 11:54
浏览 29
已采纳

单击时如何使div的<li>进入输入框?

i am making an auto complete field for my form . i am using Codigniter . my view is when run Provide a Text field for Entry of Products.

 <!-- Product AutoComplete Field -->
    <div class="form-group">
    <div class="col-md-12">
    <div class="form-group row">

    <div class="col-md-offset-2 col-md-2">     
      <label for="product" class="control-label sr_only"><?php echo lang('product_label'); ?></label>
      <?php echo form_input($productfield);?>
      <div id="category-suggestions">
        <div class="suggestions" id="category-autoSuggestionsList">    

        </div>
        <span class="help-block autocomplete"></span> 
      </div>
      <?php echo form_error('product'); ?>
    </div>
    <!-- Product Field closed -->

below is my controller it's response with some data matches from database. that are return as <li> items.

function autocomplete() {
    $this->load->model('products_model', 'product');
    $query= $this->product->entries();

    foreach($query->result() as $row):
        echo "<li title='" . $row->name . "'>" . $row->name . "</li>";
    endforeach;    
}

and this is my model function called when an input is given in the Input box.

function entries() {
      $this->db->select('name');
      $this->db->like('name', $this->input->post('queryString'), 'both'); 
      return $this->db->get('tblproducts', 10);  
 }

I am using the following jquery Code.

$(document).ready(function() {

 var item1 = '#category-suggestions';
 var item2 = '#product';
 var item3 = '#category-autoSuggestionsList';

 $(item1).hide();

 function lookup(fieldSuggestions, fieldSuggestionsList, inputString) {
      if(inputString.length == 0) {
           $(fieldSuggestions).hide();
      } else {
           $.post('http://localhost/CMS/index.php/invoice/autocomplete', 
           {queryString: ""+inputString+""}, 
           function(data){
                if(data.length >0) {
                     $(fieldSuggestions).show();
                     $(fieldSuggestionsList).html(data);
                }
        });
      }
 }

 function fill(fieldId, fieldSuggestions, thisValue) {
      $(fieldId).val(thisValue);
      setTimeout("$('" + fieldSuggestions + "').hide();", 200);
 }

 $(item2).keyup(function() {
      lookup(item1, item3, $(item2).val());
 });

 $(item3 + " li").live('click', function() { 
      fill(item2, item1, $(this).attr('title'));         
 });

  // alert("hey");
});

now my problem is my all code works correctly but i am unable to select a value from the drop down list return by controller. Even it's not clickable. Can any one help

  • 写回答

1条回答 默认 最新

  • duanbishai5271 2014-12-10 07:25
    关注

    try changing

    $(item3 + " li").live('click', function() { 
        fill(item2, item1, $(this).attr('title'));         
    });
    

    to

    $(document).on('click',item3 + " li", function() { 
        fill(item2, item1, $(this).attr('title'));         
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题