dryk50495 2014-07-27 16:19
浏览 64
已采纳

使用AJAX调用在自动完成中将变量值从一个函数传递到另一个函数

I have a problem in using autocomplete jQuery with AJAX call. When user types something in input field, I post that value with AJAX to controller where I get the values from database and then all these values to the JavaScript in success message. But after that, I'm having problem to use these JSON values for autocomplete. How to fix that?

The view file

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>         
          $(function() {

             $("#tags").autocomplete({     
                 source: function( request, response ) {  
                 $.ajax({
                          type: "POST",
                          url: "<?php echo base_url().'index.php/search_con/user' ;?>",
                          data: {'userA': request.term}, 
                   success: function(msgs)
                          {
                          var a = ["Central Palms Hotel","Hotel Monalisha","asghgjfddas","My hotel added","asdfsdf","asdsadsa"];  

                        response(a);
                         // alert(msgs);
                          }
                 });  
                  }

              });

           });


        </script>


<div class="ui-widget">
  <label for="tags">Tags:</label>
  <input id="tags">
</div>

and the controller

public function user(){

  $userPart = $_POST['userA'];      

  $result = $this->searchdb->search($userPart) ;

  $list = array();

  foreach ($result as $finaldata) {
    $data= $finaldata->name;
    array_push($list, $data);
  }

  echo json_encode($list); 
} 

Now my problem is that when i remove static variable a and use msgs for response nothing is shown in autocomplete data and when alert msgs i get the same data as in variable a. is there any mistake in my json?

  • 写回答

1条回答 默认 最新

  • doudou1897 2014-07-28 06:03
    关注

    You don't need to have separate function to fetch the data for autocomplete, can be done like this

    $(function () {
        $("#tags").autocomplete({
            source: function (request, response) {
                $.post("<?php echo base_url().'index.php/search_con/user' ;?>", {
                    userA: request.term
                }, response);
            }
        });
    });
    

    And because of the async nature of ajax you cant do this

    $.ajax({
        type: "POST",
        url: "<?php echo base_url().'index.php/search_con/user' ;?>",
        data: dataString,
        success: function (msgs) {
            data = msgs;
        }
    });
    
    alert(data); // undefined
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问1553 RT怎么测试,没有BC有方法吗
  • ¥100 业务编程如何选择学习方向和内容?
  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 【有偿】调用DXGI截图初始化失败,提示0xf 887a0004
  • ¥15 请问如何查看手机root记录?
  • ¥15 商城小程序订单号重复
  • ¥15 学校优化算法sbo和蚁群算法怎么结合
  • ¥21 matlab怎么求时域信号的二阶导数