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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog