按下第一个字符时出现Jquery自动完成问题

时间:2015-07-23 06:46:21

标签: jquery

我有一个JSON输出,如下所述

{  
   "responseHeader":{  
      "status":0,
      "QTime":29
   },
   "command":"build",
   "suggest":{  
      "mySuggester":{  
         "a":{  
            "numFound":2,
            "suggestions":[  
               {  
                  "term":"Ajay",
                  "weight":0,
                  "payload":""
               },
               {  
                  "term":"ashish kumar",
                  "weight":0,
                  "payload":""
               }
            ]
         }
      }
   }
}

但是当我按下数据的第一个字符时,我得不到响应  只有在第二或第三个字符按下时才会返回。请你帮助我  关于这个。我的jquery片段如下所示。

   $("#name" ).keyup(function(){
         var name = $("#name").val();
         $('#results').html("");

          $.ajax({
              type:"GET",
              url: "http://localhost:8983/solr/red/suggest",
              data: "suggest=true&suggest.build=true&suggest.dictionary=mySuggester&wt=json&suggest.q="+name,
              success: function (data1) {
                  console.log("data1"+data1);
                  if (data1 != null) {
                //    alert(data1);
                      var data = JSON.parse(data1);
                      console.log("JSON Parse"+data);
                      var parentNode = data.suggest.mySuggester;
                      var suggestionsNode = null;
                      for (var key in parentNode) {
                          suggestionsNode = parentNode[key].suggestions;
                          if(suggestionsNode!=null)
                              break;
                      }
                      var autocomplete_data = [];
                      $.each(suggestionsNode, function (i, val) {
                          autocomplete_data.push(val.term);
                      });
                      console.log(autocomplete_data);
                      $("#name").autocomplete({
                            source: autocomplete_data  ,
                            select: function(event,ui){

                                 $("#name").val(ui.item.label);
                                 $.ajax({
                                    type: "GET",
                                     url: "http://localhost:8080/Search/search.html",
                                     data: "name=" + ui.item.label,
                                     success: function(response){

                                            // we have the response
                                            $('#results').html(response);
                                             },
                                             error: function(e){
                                             alert('Error: ' + e);
                                         }

                                 });
                            //   return false;
                            }
                        })

                    //return false;
                  }
              },
              error: function(result) {
                  alert("Error");
              }
          })

    }); 

1 个答案:

答案 0 :(得分:2)

您没有正确使用自动填充功能。您不需要keyup事件来获取匹配项的数据。当您将自动填充的source选项设置为返回JSON的网址时,自动填充会为您执行此操作。

$("#name").autocomplete({
  // autocomplete will add &term=<your term> to this url, so you will need to handle term instead of q
  source: "http://localhost:8983/solr/red/suggest?suggest=true&suggest.build=true&suggest.dictionary=mySuggester&wt=json",
  select: function(event,ui){

    $("#name").val(ui.item.label);
    $.ajax({
      type: "GET",
      url: "http://localhost:8080/Search/search.html",
      data: "name=" + ui.item.label,
      success: function(response){

        // we have the response
        $('#results').html(response);
      },
      error: function(e){
        alert('Error: ' + e);
      }

    });
    //   return false;
  }
})