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 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号