【问题标题】:jquery auto complete inside onkey up functiononkeyup函数内的jquery自动完成
【发布时间】:2013-03-25 14:23:06
【问题描述】:

我需要附加来自 jquery 函数的结果。 当用户在搜索字段中键入时,会调用一个函数并从我的数据库中返回数据。 我可以使用警报功能查看返回的数据

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

调用的jquery函数如下

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

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

    });     
}

我需要将返回的数据作为自动完成附加,我尝试在函数中使用以下内容,我不明白为什么它不起作用

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

php文件回显数据如下

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;
    }
}

}

【问题讨论】:

  • 能否也粘贴返回数据的格式?

标签: php jquery jquery-autocomplete


【解决方案1】:

您需要格式化 db 调用的输出:

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

然后,你需要解析返回的数据。

var sourceData = [];
var arrData = $.parseJSON(data);
foreach(x in arrData)
{
    sourceData.push(arrData[x]);
}
$( "#symbol" ).autocomplete({
            source: sourceData
});

【讨论】:

  • 他还需要记住他的数据需要格式化为:[ { label: "Choice1", value: "value1" }, ... ] 或 [ "Choice1", "Choice2 " ]
  • 谢谢你,需要等待 3 小时才能发布我的答案,因为我是新来的。
【解决方案2】:

试试这个

$( "#symbol" ).autocomplete({
                'search':function(event,ui){
                var newUrl="portfolio/searchStock.php/abc/"+$("#symbol").val();
                $(this).autocomplete("option","source",newUrl)
                },
                'source':[]
    });  

在php中

  function abc($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)){

       $new_row['label']=htmlentities(stripslashes($query_row['symbol']));
     $new_row['value']=htmlentities(stripslashes($query_row['symbol_id']));
    $row_set[] = $new_row; //build an array
  }

 }echo json_encode($row_set);
}

【讨论】:

    【解决方案3】:

    感谢您的回复,这似乎有效。需要从 php 端编码和解析的数组 JavaScript 文件

        function findmatch(){   
        var symbol= document.getElementById("tSymbol").value;   
        $.post("portfolio/searchStock.php",
        {
          search:symbol
        },
        function(data,status){
        var arrData = $.parseJSON(data);
        $("#tSymbol").autocomplete({
             source: arrData
        });
        });     
    }
    

    searchStock.php

    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 json_encode($symbol);
    
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多