dpxkkhu1812 2015-02-01 19:24
浏览 63
已采纳

PHP数组和jQuery自动完成不能处理第一个字符

My autocomplete plugin from jQuery is not working first time with one character in the textbox.
If I continue to type more than one character then the autocomplete kicks in...
So I'm expecting result from the first character but is not getting anything..

<html>
<head>
    <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
    <script type="text/javascript">

        $(document).ready(function() {
            $( "#names" ).keyup(function() {
                autocompleteNames();
            });
        });

        function loadNames(){
            //Gets the name
            var nameSelected = $("#names").val();
            var namesList = "";
            $.ajax({
                url: 'names.php',
                type: "POST",
                async: false,
                data: { sport: nameSelected}
            }).done(function(names){
                namesList = names.split(',');
            });
            //Returns the javascript array of names.
            return namesList;
        }
        function autocompleteNames(){
            var names = loadNames();
            $("#names").autocomplete({
                source: names,
                minLength: 1
            });
        }
    </script>
</head>
<body>
Namn: <input type="text" id="names" name="names" />
</body>
</html>

And here's my names.php file that's very simple

<?php
$sport = $_POST["sport"];
//Defines the name  array.
$names[0] = "Sam";
$names[1] = "Anna";
$names[2] = "Jens";
$names[3] = "Johanna";
$names[4] = "Emma";
$names[5] = "Mikael";
$names[6] = "Mattias";
$names[7] = "Sebastian";
$names[8] = "Johan";
$names[9] = "Mona";
$names[10] = "Lina";
$names[11] = "Linda";
$names[12] = "Ebba";
$names[13] = "Andreas";
$names[14] = "Marcus";
$names[15] = "Markus";
$names[16] = "Anders";
$names[17] = "Maria";
$names[18] = "Sandra";
$names[19] = "Jonatan";
$names[20] = "Jacob";
$names[21] = "Carolina";
$names[22] = "Tom";
$names[23] = "Tim";
$names[24] = "Zlatan";
$names[25] = "Emelie";

//Defines an empty variable that will return the javascript array.
$teams = generateAutocompleteArray($names);

//Returns the teams in the appropriate javascript array format.
echo $teams;

//Function converts PHP array a string where it can be split into an array easily.
function generateAutocompleteArray($namesArray){
    $jsNamesArray = "";

    $teamCount = count($namesArray);
    for($i=0; $i<$teamCount; $i++){
        $jsNamesArray.= $namesArray[$i].',';
    }
    //Removes the remaining comma so you don't get a blank autocomplete option.
    $jsNamesArray = substr($jsNamesArray, 0, -1);

    return $jsNamesArray;
}
?>
  • 写回答

1条回答 默认 最新

  • douhuai4155 2015-02-01 19:37
    关注

    I don't think you should bind your autocomplete constructor to the keyup event of the input text. If so, you are recreating the object every time the user types something. Also, you are returning the namesList and maybe it is still loading from the ajax request. You have to wait for the request to complete in order to have the namesList, remember that javascript is asynchronous.

    So basically, you have to construct the autocomplte just once, and look for the autocomplete parameters (source) to specify that your data is loaded via ajax. Take a look at this: jQuery autocomplete with callback ajax json

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料