2014-10-06 92 views
0

我正在嘗試搜索地址表單。爲什麼這個ajax代碼有延遲?

這是結構和方法。

search_address.html:

<form id="search_address_form" onSubmit="sendAddress(); return false;"> 
    <input id="input_address" class="input_address" name="input_address" type="text" placeholder="example: blahblahblah"/> 
</form> 
<div id="addressList_layer" class="addressList_layer"> 

<script> 
    input_address = document.getElementById('input_address'); 
    input_address.addEventListener('keydown',function(){  
     sendAddress();  
    }); 

    function sendAddress(){ 
     xhrDocOpen('./php/search_address.php?searchingFor='+inputAddress_data.value,'addressList_layer','get'); 
     return false; 
    } 
</script> 

search_address.php:

<?php 
include_once('./config.php'); 
extract($_GET); 
echo ($searchingFor); 
?> 

此代碼工作正常。

但它與延遲點點運行...

我的意思是,例如,

input_address.value : "1" 
addressList_layer.value : "" 

input_address.value : "123" 
addressList_layer.value : "12" 

input_address.value : "123456" 
addressList_layer.value : "12345" 

input_address.value : "123abc" 
addressList_layer.value : "123ab" 

這樣,addressList_layer顯示總是一步比input_address延遲。

我不知道爲什麼......

是否有任何人誰可以猜測出現這種情況的原因,並給我解決?

PS。 xhrDocOpen()是...

var xhr = new XMLHttpRequest(); 
function xhrDocOpen(doc,placeID,method){ 
    xhr.onreadystatechange=function(){ 
     if(xhr.readyState==4 && xhr.status==200){ 
      document.getElementById(placeID).innerHTML=xhr.responseText; 
     } 
    xhr.open(method,doc,true); 
    xhr.send(''); 
} 

回答

1

這是通常的問題,如果你想使用 「的keydown」 事件。

只是試圖將其更改爲「鍵入」,它應該工作。

+0

謝謝。但你怎麼知道這是一個常見問題?僅僅是因爲這些問題在這裏發佈了很多? – json84 2014-10-06 12:39:11