1 回答

TA貢獻1826條經(jīng)驗 獲得超6個贊
您需要發(fā)送一個 AJAX 請求,您需要確保這responseType是text(這是默認的,順便說一句)并this.responseText在onreadystatechange回調中使用,如下例所示:
<!DOCTYPE html>
<html>
<head>
<script>
function showHint(str) {
? if (str.length == 0) {
? ? document.getElementById("txtHint").innerHTML = "";
? ? return;
? } else {
? ? var xmlhttp = new XMLHttpRequest();
? ? xmlhttp.onreadystatechange = function() {
? ? ? if (this.readyState == 4 && this.status == 200) {
? ? ? ? document.getElementById("txtHint").innerHTML = this.responseText;
? ? ? }
? ? }
? ? xmlhttp.open("GET", "gethint.php?q="+str, true);
? ? xmlhttp.send();
? }
}
</script>
</head>
<body>
<p><b>Start typing a name in the input field below:</b></p>
<form action="">
? <label for="fname">First name:</label>
? <input type="text" id="fname" name="fname" onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
- 1 回答
- 0 關注
- 98 瀏覽
添加回答
舉報