2011-09-03 87 views
0

我有一個意見箱的腳本。它的工作很好,但與此問題我想修改它的upkey和downkey事件。它只是提供建議甚至不適用於鼠標點擊事件。如何修改這個jQuery?

function lookup(inputString) { 
    if(inputString.length == 0) { 
     // Hide the suggestion box. 
     $('#suggestions').hide(); 
    } else { 
     $.post("rpc.php", {queryString: ""+inputString+""}, function(data){ 
      if(data.length >0) { 
       $('#suggestions').show(); 
       $('#autoSuggestionsList').html(data); 
      } 
     }); 
    } 
} // lookup 

function fill(thisValue) { 
    $('#inputString').val(thisValue); 
    setTimeout("$('#suggestions').hide();", 50); 
} 

HTML

<input type="text" size="30" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" /> 
<div class="suggestionsBox" id="suggestions" style="display: none;"> <img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" /> 
<div class="suggestionList" id="autoSuggestionsList"> &nbsp; </div> 

由於我是新的jQuery,可能我的想法如何進一步修改它點擊鼠標或鍵盤方向鍵。

回答

1

爲什麼重新發明輪子 - 你看過jQuery UI自動完成? http://jqueryui.com/demos/autocomplete/

+0

能否請您提供思路它只搜索第一個字符。假設在這個例子中,如果我鍵入'JA',它給了我3個值,即「AJAX,JAVA,JAVASCRIPT」我只想得到Java和JavaScript我不希望獲取中間有這個字符串的記錄。 –

+0

所以你想要一個開始搜索而不是一個包含。已經有這方面的例子。例如。 http://stackoverflow.com/questions/3148195/jquery-ui-autocomplete-use-startswith – Rezler

1

我覺得這可能會幫助您用鍵盤的箭頭鍵(註冊鍵事件處理)

// Handling keybinding keydown. 
$(document).keydown(function(e){ 
     switch(e.keyCode){ 
      case 38: //this is (UP) pressed down 
      // do the action you want. 
       break; 
     } 
}); 

// Handling keybinding keyup. 
$(document).keydown(function(e){ 
     switch(e.keyCode){ 
      case 38: //this is (UP) pressed down 
      // do the action you want. 
       break; 
     } 
}); 

這個鏈接將幫助您的鍵碼:

http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/javascript-char-codes-key-codes.aspx

+0

對不起,但我沒有任何效果,如果我使用此代碼。仍然沒有效果我不能使用鼠標點擊或光標鍵選擇值。 –