2011-12-12 68 views
0

從我的特定問題我已經得到了實現自動完成列表的答案..但問題是當我點擊任何建議它空白我的輸入框。我有輸入框類track。我的功能,我用於在模糊事件是..組文本框的自動完成列表

<input name="track[]" type="text" class="track" maxlength="150"/> 
<div class="suggestionsBox" id="suggestions" style="display: none;"> 
    <img src="images/upArrow.png" style="position: relative; top: -12px; left: 50px;" alt="upArrow" /> 
    <div class="suggestionList" id="autoSuggestionsList">&nbsp;</div> 
</div> 

<script> 
$('input.track').keyup(function() { 
var poetname = $(this).val(); 
if(poetname.length == 0) { 
    // Hide the suggestion box. 
    $(this).parent().find('#suggestions').hide(); 
} 
else { 
    $.ajax({ 
     url: "rpc.php", 
     type: "post", 
     data: {queryString: poetname}, 
     context: this, // i.e. the text box 
     success: function(data) { 
      if(data.length >0) { 
       $(this).parent().find('#suggestions').show(); 
       $(this).parent().find('#autoSuggestionsList').html(data); 
      } 
     } 
    }); 
} 
}); 

$('input.track').blur(function() { 
    $(this).val(); 
    $(this).parent().find('.suggestionsBox').hide(); 
}); 
</script> 
+0

中的代碼不能(並且不)做你的描述。請提供更完整的代碼示例,其中包括實際更新輸入框的部分。 FWIW,你的'$(this).val();'目前什麼都不做。 –

+0

我已經編輯了自動列表的關鍵事件。希望它會有所幫助 –

回答

0

兩件事情正在進行這裏 -

  1. 我不知道你捕捉你想捕捉的事件。我無法確定用例是什麼,但您似乎在說您正在傾聽點擊某個建議。但是,您並未在建議框的任何位置捕獲任何點擊。

  2. 模糊事件只隱藏.suggestionsBox,如果這就是它的意思,那麼它工作的很好! $(this).val();行沒有做任何有用的事情。 JavaScript實際上找到了這個價值,但它沒有對它做任何事情。

無論如何,我沒有看到輸入框被空白。

這是一個使用測試回聲阿賈克斯小提琴:http://jsfiddle.net/xXF9v/