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条)

报告相同问题?

悬赏问题

  • ¥20 安装 opencv4nodejs 报错
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!