📄 ajax_example_suggest.asp
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>AJAX Suggest Example</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<link rel="stylesheet" type="text/css" href="../stdtheme.css" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
<script src="clienthint.js" type="text/javascript"></script>
</head>
<body>
<a name="top"></a>
<table cellpadding="0" cellspacing="0" width="100%" bgcolor="#808080">
<tr>
<td width="234" valign="top">
<a href="../default.htm"><img src="../images/w3default80.jpg" border="0" alt="W3Schools" /></a>
</td>
<th valign="middle" align="left" class="right">
<iframe style="background-color:#808080" src="../banners/bannerframe.asp@adpartner=w3markup" height="90" width="728"
marginwidth="0" marginheight="0" frameborder="0" scrolling="no">
Your browser does not support inline frames or is currently configured not to display inline frames.
</iframe>
<br /></th>
</tr>
</table>
<table style="margin-left:1px" width="800px" border="0" cellpadding="0" cellspacing="2">
<tr>
<td width="137" class="content" valign="top"><br />
<a class="left" href="../default.asp" target="_top">
<img src="../images/homeicon.gif" border="0" alt="home" /></a>
<a class="left" href="../default.asp" target="_top">
<b>HOME</b></a>
<br /><br />
<b>AJAX Basic</b><br />
<a class="left" target="_top" href="default.asp" >AJAX HOME</a><br />
<a class="left" target="_top" href="ajax_intro.asp" >AJAX Intro</a><br />
<a class="left" target="_top" href="ajax_httprequest.asp" >AJAX HTTP Request</a><br />
<a class="left" target="_top" href="ajax_example.asp" >AJAX Example</a><br />
<a class="left" target="_top" href="ajax_browsers.asp" >AJAX Browsers</a><br />
<a class="left" target="_top" href="ajax_xmlhttprequest.asp" >AJAX XMLHttpRequest</a><br />
<a class="left" target="_top" href="ajax_server.asp" >AJAX Server</a><br />
<a class="left" target="_top" href="ajax_serverscript.asp" >AJAX Server Script</a><br />
<br />
<b>AJAX Advanced</b><br />
<a class="left" target="_top" href="ajax_example_suggest.asp" style='font-weight:bold;'>AJAX Suggest</a><br />
<a class="left" target="_top" href="ajax_source.asp" >AJAX Source</a><br />
<a class="left" target="_top" href="ajax_database.asp" >AJAX Database</a><br />
<a class="left" target="_top" href="ajax_xmlfile.asp" >AJAX XML File</a><br />
<a class="left" target="_top" href="ajax_responsexml.asp" >AJAX ResponseXML</a><br />
<br />
<b>AJAX Examples</b><br />
<a class="left" target="_top" href="ajax_examples.asp" >AJAX Examples</a><br /><br />
<b>Selected Reading</b><br />
<a class="left" target="_top" href="../browsers/browsers_stats.asp">Web Statistics</a><br />
<a class="left" target="_top" href="../site/site_glossary.asp">Web Glossary</a><br />
<a class="left" target="_top" href="../hosting/default.asp">Web Hosting</a><br />
<a class="left" target="_top" href="../quality/default.asp">Web Quality</a><br /><br />
<a class="left" target="_top" href="../forum/default.asp">W3Schools Forum</a><br /><br />
<a class="left" target="_top" href="../about/about_helping.asp">Helping W3Schools</a><br /><br />
<script type="text/javascript"><!--
google_ad_client = "pub-3440800076797949";
/*LeftLinkUnit*/
google_ad_slot = "4854527104";
google_ad_width = 120;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="../../pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td>
<td valign="top">
<table style="border: 1px solid gray" width="100%" bgcolor="#FFFFFF" border="0" cellpadding="5" cellspacing="0">
<tr>
<td>
<h1>AJAX Suggest Example</h1>
<a href="ajax_serverscript.asp"><img border="0" src="../images/btn_previous.gif" width="100" height="20" alt="Previous" /></a>
<a href="ajax_source.asp"><img border="0" src="../images/btn_next.gif" width="100" height="20" alt="Next" /></a>
<hr />
<p class="intro">We have seen that AJAX can be used to create more interactive applications. </p>
<hr />
<h2>AJAX Suggest Example</h2>
<p>In the AJAX example below we will demonstrate how a web page can communicate
with a web server online as a user enters data into a standard HTML form. </p>
<hr />
<h2>Type a Name in the Box Below</h2>
<form action="">
First Name:
<input type="text" id="txt1"
onkeyup="showHint(this.value)" />
</form>
<p>Suggestions: <span id="txtHint"></span></p>
<hr />
<h2>Example Explained - The HTML Form</h2>
<p>The form above has the following HTML code:</p>
<table class="ex" width="100%" border="1" id="table1"><tr><td>
<pre><form>
First Name:
<input type="text" id="txt1"
onkeyup="showHint(this.value)">
</form></pre>
<pre><p>Suggestions: <span id="txtHint"></span></p> </pre>
</td></tr></table>
<p>
As you can see it is just a simple HTML form with an input
field called "txt1".</p>
<p>
An event attribute for the input field defines a function to be triggered by the
onkeyup event. </p>
<p>The paragraph below the form contains a span called "txtHint". The span is
used as a placeholder for data retrieved from the web server.</p>
<p>When the user inputs data, a function called "showHint()" is executed. The
execution of the function is triggered by the "onkeyup" event. In other words:
Each time the user moves his finger away from a keyboard key inside the input
field, the function showHint is called.</p>
<hr />
<h2>Example Explained - The showHint() Function</h2>
<p>The showHint() function is a very simple JavaScript function placed in the
<head> section of the HTML page.</p>
<p>The function contains the following code:</p>
<table class="ex" width="100%" border="1" id="table2"><tr><td>
<pre>function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="gethint.asp";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}</pre>
</td></tr></table>
<p>The function executes every time a character is entered in the input field.</p>
<p>If there is some input in the text field (str.length > 0) the function
executes the following:</p>
<ul>
<li>Defines the url (filename) to send to the server</li>
<li>Adds a parameter (q) to the url with the content of the input field </li>
<li>Adds a random number to prevent the server from using a cached file</li>
<li>Creates an XMLHTTP object, and tells the object to execute a function
called stateChanged when
a change is triggered</li>
<li>Opens the XMLHTTP object with the given url.</li>
<li>Sends an HTTP request to the server</li>
</ul>
<p>If the input field is empty, the function simply clears the content of the txtHint
placeholder.</p>
<hr />
<h2>Example Explained - The GetXmlHttpObject() Function</h2>
<p>The example above calls a function called GetXmlHttpObject().</p>
<p>The purpose of the function is to solve the problem of creating different
XMLHTTP objects for different browsers. </p>
<p>The function is listed below:</p>
<table class="ex" width="100%" border="1" id="table4"><tr><td>
<pre>function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -