我一直在關注以下問題,並希望你們能指點我(可能很明顯)的解決方案。使用AJAX刷新另一個div後,將焦點設置在textfield上
我有一個簡單的Web應用程序,它需要輸入一個輸入字段(type =「text」)。當頁面加載時,光標已準備好接受輸入。只要用戶導航(onBlur),AJAX部分就會啓動,我會查找數據庫中的代碼,並根據結果右側的框變爲綠色或保持紅色。
更改後,我希望輸入字段清零並準備好接收新輸入。然而,清除該領域確實有效,但重點不在此列。我嘗試了focus()和select(),但是沒有去...我試圖在setTimeout中摺疊,不行。使用當前的代碼,它在FF,Chrome和IE中不起作用。我希望你們能給我一個提示。
這是網頁代碼:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function checkTicket(tno) {
var xmlhttp;
if (tno=='') {
document.getElementById('inside').innerHTML='0';
document.getElementById('validbox').style.backgroundColor='#FF0000';
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById('inside').innerHTML=xmlhttp.responseText;
if (document.getElementById('inside').innerHTML=='1') {
document.getElementById('validbox').style.backgroundColor='#00FF00';
} else {
document.getElementById('validbox').style.backgroundColor='#FF0000';
}
set_focus();
}
}
xmlhttp.open("GET","check_eticket.php?t="+tno,true);
xmlhttp.send();
}
function set_focus() {
document.form.ticketnumber.value='';
document.form.ticketnumber.focus();
}
</script>
</head>
<body onLoad="set_focus()">
<div id="wrapper" style="position: absolute; top: 50%; height: 400px; margin-top: -200px; width: 99%;">
<div id="innerwrap" style="width: 75%; margin-left: auto; margin-right: auto;">
<div style="float: left; width: 50%; margin-left: auto; height: 400px; padding-top: 115px;">
<span style="font-size: 3em; font-weight: bold; text-align: center;">TOEGANGSCODE:<br /></span>
<form name="form">
<input name="ticketnumber" id="ticketnumber" type="text" onBlur="checkTicket(this.value)" size="11" style="font-size: 3.55em; font-weight: bold;" />
</form>
</div>
<div id="validbox" style="float: right; width: 50%; background-color: #FF0000; height: 400px; margin-right: auto;">
<div id="inside" style="display: none;">0</div>
</div>
</div>
</div>
</body>
</html>
嘗試'的setTimeout(函數(){document.form.ticketnumber.focus();},1);'值變化可以觸發導致要失去焦點瀏覽器一些行動。 – Prusse
我試着用FF和IE的最新版本,它爲我工作。如果你使用FF,那麼我猜你也使用螢火蟲?如果您將控制檯設置爲「顯示嚴格的錯誤消息」並啓用「show XMLHttpRequest」,那麼您可能會發現一些內容。 –
** TRY的jQuery, 使用這個鏈接作爲樣品** [http://stackoverflow.com/questions/1433742/set-focus-to-field-in-div][1] [1]:http://stackoverflow.com/questions/1433742/set-focus-to-field-in-div –