0
以下(縮寫)代碼與輸入框中的自動完成類似,將結果輸出到輸入框下方的div中。它可以在Chrome/Firefox中完美地工作(即搜索「Eggs」,然後「Milk」顯示他們的結果),但是在IE中它使第一個請求很好,但是它不會輸出任何進一步的請求。多個請求在IE中未呈現的AJAX輸出
網絡顯示IE正在向服務器發送後續請求(200頭代碼),但是它使用它做任何事情。
編輯:我不能使用jQuery。但可以使用YUI3(平臺限制)
/* Setup Ajax */
function ajaxRequest(){var activexmodes=["Msxml2.XMLHTTP", Microsoft.XMLHTTP"],i;if(window.ActiveXObject){for(i=0; i<activexmodes.length; i++){try{return new ActiveXObject(activexmodes[i]);}catch(ignore){/*suppress error*/}}}else if (window.XMLHttpRequest){return new XMLHttpRequest();}else{return false;}}
/* Get Value of search */
var searchBoxObj = document.getElementById('searchBox-1');
/* The Ajax Request */
var theSearchValue;
var mygetrequest=new ajaxRequest();
mygetrequest.onreadystatechange=function(){
if (mygetrequest.readyState==4){
if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
var jsondata=JSON.parse(mygetrequest.responseText), /* retrieve result as an JavaScript object */
searchData = jsondata.searches,
i;
if (searchData.length > 0){
document.getElementById('result').className = "on";
output='<ul id="searchResults">';
for (i=0; i < searchData.length; i++){
/* The Loop Code */
}
output+='</ul>';
}
else {
document.getElementById('result').className = "";
}
document.getElementById("result").innerHTML = output;
}
else{
/* alert("An error has occured making the request") */
}
}
};
/* With every key press */
var stoppedTyping;
searchBoxObj.onkeyup = function() {
if (stoppedTyping) { clearTimeout(stoppedTyping);}
stoppedTyping = setTimeout(function(){
if (searchBoxObj.value.length > 2){
theSearchValue = searchBoxObj.value;
mygetrequest.open("GET", "/asp/lookup.asp?term="+theSearchValue, true);
mygetrequest.send(null);
}
}, 200);
};
感謝您的回答,但是我不能在該平臺上使用jQuery。我可以使用YUI3然而, – acasperw
嘿好吧我的壞,我認爲這可能是一個問題,在IE中的事件處理可能已被糾正使用jQuery;) – mazuno