自动完成Ajax密钥始终未定义

时间:2018-05-14 13:54:03

标签: javascript ajax autocomplete

我正在使用自动填充功能,当我删除 phpspellcheck api时,它可以正常工作。但是当我使用它。它总是说键未定义。 我想在回应我的拼写检查之前api ajax运行。我如何运行第一个拼写检查api然后ajax。

$("#school-searchbox-wrapper > .search").autocomplete({
                source: function (request, response) {
                    var keys = request.term;
                    //console.log(key);
                    var mySpellInstance = new LiveSpellInstance()   
                    mySpellInstance.AjaxDidYouMean(request.term);
                    mySpellInstance.onDidYouMean = function(suggestion, original){
                        var message="";
                        if(suggestion!=""){
                            var key = suggestion; 
                            //alert(key);     
                        }else{
                                var key = keys;
                        }
                    }

                    console.log(key);
                    $.ajax({
                        async: false,
                        url: SITE_URL + "books/search/school",
                        dataType: "json",
                        data: {
                            q: key
                        },
                        success: function (data) {
                            console.log(data);
                            response(data.response);
                        }
                    });
                },
                focus: function (event, ui) {
                    //$( "#search" ).val( ui.item.title ); // uncomment this line if you want to select value to search box  
                    return false;
                },
                select: function (event, ui) {
                    window.location.href = "book-detail.php?id=" + ui.item.id;
                }
            }).data("ui-autocomplete")._renderItem = function (ul, item) {
                var inner_html = "<div class='ui-menu-item-wrapper'>" + item.name + "</div>";
                return $("<li></li>")
                        .data("item.autocomplete", item)
                        .append(inner_html)
                        .appendTo(ul);
            }

0 个答案:

没有答案