douchun3680 2013-03-25 14:23
浏览 64
已采纳

onkeyup函数中的jquery自动完成

i need to attach results from a jquery function. when the user types in the search field, a function is called and data is returned from my database. i can see the returned data using alert function

<input type="text" name="symbol" id="symbol" required="required"  onkeyup="findmatch();">

The Jquery function called is below

    function findmatch(){
    var symbol= document.getElementById("symbol").value;

    $.post("portfolio/searchStock.php",
    {
      search:symbol
    },
    function(data,status){
    alert(data);

    });     
}

i need the data returned to be attached as an auto comlete, i have tried using the following within the function, i dont understand why it doesnt work

$( "#symbol" ).autocomplete({
                source: data
        });

The php file echoes data as below

if (isset ($_POST['search'])){

$search = $_POST['search']; if(!empty ($search)){ $query="select * from companylist where symbol like '".mysql_real_escape_string($search)."%'"; $query_run = mysql_query($query);

    while ($query_row = mysql_fetch_assoc($query_run)){
        $symbol = $query_row['symbol'];
        echo $symbol;
    }
}

}

  • 写回答

3条回答 默认 最新

  • drdr123456 2013-03-25 14:29
    关注

    You need to format the output of the db call:

    while ($query_row = mysql_fetch_assoc($query_run)){
        $symbol[] = $query_row['symbol'];
    }
    echo json_encode($symbol);
    

    Then, you need to parse the returned data.

    var sourceData = [];
    var arrData = $.parseJSON(data);
    foreach(x in arrData)
    {
        sourceData.push(arrData[x]);
    }
    $( "#symbol" ).autocomplete({
                source: sourceData
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记