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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题