我是Javascript的绝对新手,所以我很抱歉。我有一个适应和工作的自动完成脚本,但当我点击屏幕上的其他地方或远离输入字段的选项卡时,结果列表不会消失。我一直在尝试不同的方法几个小时没有运气。
function findCustomer(thevalue, e) {
if (thevalue.length > 1) {
theObject = document.getElementById("autocompletediv");
theObject.style.visibility = "visible";
theObject.style.width = "152px";
var posx = 0;
var posy = 0;
posx = (findPosX (document.getElementById("surname")) +200);
posy = (findPosY (document.getElementById("surname")) +0);
theObject.style.left = posx + "px";
theObject.style.top = posy + "px";
var theextrachar = e.which;
if (theextrachar == undefined){
theextrachar = e.keyCode;
}
var objID = "autocompletediv";
if (thevalue.length == 1) {
var serverPage = "findcustomer.php";
} else {
var serverPage = "findcustomer.php" + "?sn=" + thevalue.substr (0, (thevalue.length));
}
var obj = document.getElementById(objID);
var request = new XMLHttpRequest();
request.open("GET", serverPage);
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
obj.innerHTML = request.responseText;
}
}
request.send(null);
} else {
theObject.style.visibility = "hidden";
}
}
这样可以在输入2个或更多字符时显示div,当输入1个字符或没有字符时隐藏。
我认为onBlur应该有效,所以我尝试了以下内容:
<td><input name="surname" type="text" class="short" id="surname" autocomplete="off" onKeyUp="findCustomer(this.value, event)" onBlur="hideDiv()" /></td>
功能:
function hideDiv() {
document.getElementById('autocompletediv').style.visibility = "hidden";
}
我确信你们大多数人在哪里出错都很明显......