2011-06-23 113 views
0

我已經設置了一個jQuery自動完成,它根據文本框上的輸入更改數據源。jquery自動完成數據源更改

在jquery上的數據源更改後,直到按下向上或向下箭頭按鈕纔會觸發。

我用firebug來檢查數據源,我找不到任何錯誤。

有人可以告訴我如何發送向上或向下箭頭鍵控制或解決這個以任何其他方式?

非常感謝!

編輯:如下我與JSON代替這一點,但它似乎提出請求的錯誤提示框

jQuery(function() { jQuery("input#autocomplete").autocomplete({ contentType: 'application/json; charset=utf-8', dataType: 'json', mustMatch: false, limit: 10, minChars: 2,

  select: function (event, ui) { 
       AutoCompleteSelectHandler(event, ui) 
      } 
      , 
      source: function (request, response) { 
       jQuery.ajax({ 
        url: "http://localhost/integration/webservices/PostcodeJSON.asmx/GetPostCodeListJSONfromSuburb", 
        data: {}, 
        dataType: "json", 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        dataFilter: function (data) { return data; }, 
        success: function (data) { 
         alert(data); 
        }, 
        error: function (XMLHttpRequest, textStatus, 

errorThrown) { alert(textStatus); } }); } });

}); 

有這個網站輸入框。

我在這裏做了什麼錯?我已確認Web服務正常工作。

EDIT2:我已經喜歡以下更改:

jQuery(function() { jQuery("input#autocomplete").autocomplete({

  minChars: 2, 

      select: function (event, ui) { 
       AutoCompleteSelectHandler(event, ui) 
      } 
      , 
      source: function (request, response) { 
       jQuery.ajax({ 
        url: "http://localhost/integration/webservices/PostcodeJSON.asmx/GetPostCodeListJSONfromSuburb", 
        data: '{ Suburb: "' + 

jQuery("#autocomplete").val() + '" }', dataType: "json", type: "POST", contentType: "application/json; charset=utf-8", dataFilter: function (data) { return data.d; }, success: function (data) { alert(data.d); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(textStatus); } }); } });

}); 

因此警報工作正常。但jquery不顯示匹配的列表。我該怎麼做呢?

編輯2:

我設法解決了webservice的問題。如何設置響應以便自動完成相應地顯示列表?目前,列表中的每個項目都顯示了完整的項目列表。如果我輸入「ab」,並且如果有3件事匹配,那麼它會在3條不同的行上向我顯示相同的結果3次。

我有jQuery的設置類似如下:

的jQuery(函數(){ 的jQuery( 「輸入#自動完成」)自動完成({

  minChars: 2, 

      select: function (event, ui) { 
       AutoCompleteSelectHandler(event, ui) 
      } 
      , 
      source: function (request, response) { 
       jQuery.ajax({ 
        url: "http://localhost/integration/webservices/PostcodeJSON.asmx/GetPostCodeListJSONfromSuburb", 
        data: '{ Suburb: "' + jQuery("#autocomplete").val() + '" }', 
        dataType: "json", 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        dataFilter: function (data) { return data; }, 
        success: function (data) { 

                response($.map(data.d, function (item) { 
                 return { 
                  value: data.d 
                 } 
                       })) 


        }, 
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
         alert(textStatus); 
        } 
       }); 
      } 
     }); 

任何幫助將非常感激,非常感謝!

+0

你的描述使得幾乎沒有任何意義(對我來說)。例如,'在jQuery的數據源後,直到按上或下箭頭按鈕纔會觸發。什麼是'jquery'上的'datasource'?是什麼導致它改變的?什麼是'它'沒有激發?沒有共享代碼,幫助你關於不可能幫助某人說「我正在使用自動填充功能,但它不起作用,請幫助」。 – davin

+0

我使用xmlhttpobject上的請求從頁面獲取列表, 因此數據源只是一個字符串數組。 如果我更改數據源,自動完成列表不顯示。但是,當我按向上箭頭鍵或向下箭頭時,列表顯示出來。 也許我應該嘗試使用json與自定義數據源。 – rlee923

+0

仍然不清楚,當然不可重現。如果你設置了一個http://jsfiddle.net它將使每個人的生活變得更容易,並且解決你的問題的機會 – davin

回答

0

我得到它的工作,但有一件事我不確定的是,該項目變成只是一個字符串數組而不是JSON對象我試圖解析每個項目作爲JSON,但似乎並沒有工作。

這裏是工作的jQuery json與webservice jquery組合。

的jQuery(函數(){ 的jQuery( 「輸入#自動完成」)。自動完成({

  minChars: 2, 

      select: function (event, ui) { 
       AutoCompleteSelectHandler(event, ui) 
      } 
      , 
      source: function (request, response) { 
       jQuery.ajax({ 
        url: "http://localhost/integration/webservices/PostcodeJSON.asmx/GetPostCodeListJSONfromSuburb", 
        data: '{ Suburb: "' + jQuery("#autocomplete").val() + '" }', 
        dataType: "json", 
        type: "POST", 
        minChars: 2, 
        contentType: "application/json; charset=utf-8", 
        dataFilter: function (data) { return data; }, 
        success: function (data) { 
         var obj = jQuery.parseJSON(data.d); 
         response($.map(obj, function (item) { 
          var item_obj = jQuery.parseJSON(item); 
          return { 
           value: item[1] 


          } 
         })) 


        }, 
        //      parse: function (data) { 
        //       var parsed = []; 
        //       data = data.d; 

        //       for (var i = 0; i < data.length; i++) { 
        //        parsed[parsed.length] = { 
        //         data: data[i], 
        //         value: data[i].value, 
        //         result: data[i].value 
        //        }; 
        //       } 

        //       return parsed; 
        //      }, 
        //      formatItem: function (item) { 
        //       return item.value; 
        //      }, 
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
         alert(textStatus); 
        } 
       }); 
      } 
     }); 



    });