<!doctype html> <html> <meta charset="utf-8"> <title>ajax</title> <body> <h2> xmlhttprequest 对象 </h2> <h3>开始在下面的input字段中键入名称:</h3> <p>建议: <span id="txthint"></span></p> <p>first name: <input type="text" id="txt1" onkeyup="showhint(this.value)"></p> <script> function showhint(str) { var xhttp; if (str.length == 0) { document.getelementbyid("txthint").innerhtml = ""; return; } xhttp = new xmlhttprequest(); xhttp.onreadystatechange = function() { if (this.readystate == 4 && this.status == 200) { document.getelementbyid("txthint").innerhtml = this.responsetext; } }; xhttp.open("get", "/jc_script/gethint.php?q="+str, true); xhttp.send(); } </script> </body> </html>
在上面的示例中,当用户在输入字段中键入字符时,showhint()执行被调用的函数。该函数由onkeyup事件触发。
str变量保存input字段的内容
<?php// 带名字的数组 $a[] = "anna";$a[] = "brittany"; $a[] = "cinderella"; $a[] = "diana"; $a[] = "eva"; $a[] = "fiona"; $a[] = "gunda"; $a[] = "hege"; $a[] = "inga"; $a[] = "johanna"; $a[] = "kitty"; $a[] = "linda"; $a[] = "nina"; $a[] = "ophelia"; $a[] = "petunia"; $a[] = "amanda"; $a[] = "raquel"; $a[] = "cindy"; $a[] = "doris"; $a[] = "eve"; $a[] = "evita"; $a[] = "sunniva"; $a[] = "tove"; $a[] = "unni"; $a[] = "violet"; $a[] = "liza"; $a[] = "elizabeth"; $a[] = "ellen"; $a[] = "wenche"; $a[] = "vicky"; // 从url获取q参数 $q = $_request["q"]; $hint = ""; // 如果$q不等于"",则从数组中查找所有提示 if ($q !== "") { $q = strtolower($q); $len=strlen($q); foreach($a as $name) { if (stristr($q, substr($name, 0, $len))) { if ($hint === "") { $hint = $name; } else { $hint .= ", $name"; } } } } // 如果未找到提示或输出正确值,则输出“无建议” echo $hint === "" ? "无建议" : $hint;
如对本文有疑问, 点击进行留言回复!!
《Android移动应用基础教程》(Android Studio)(第二版)黑马教程 课后题答案 第11章
【我的架构师之路】- go1.14.16源码分析笔记之源码目录
网友评论