ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: AJAX :: AJAX Suggest °¸Àý

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

AJAX
AJAX ʵÀý
AJAXä¯ÀÀÆ÷
AJAXÔ´´úÂë
AJAX·þÎñ¶Ë
AJAX Êý¾Ý¿â
AJAXÓëXMLÎļþ
AJAX XMLHttpRequ..
΢ÈíµÄAjax
AJAX ½éÉÜ
AJAX HTTP ÇëÇó
AJAX ·þÎñÆ÷¶Ë½Å±..
AJAX Suggest °¸À..
AJAX ResponseXML
AJAX AppML

AJAX Suggest °¸Àý


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-08-23 ä¯ÀÀ:3017 :: ::

We have seen that AJAX can be used to create more interactive applications.
ÎÒÃÇÒѾ­·¢ÏÖ£¬AJAX¿ÉÒÔÓÃÓÚ´´½¨¸ü¾ß½»»¥ÐÔµÄÓ¦ÓóÌÐò¡£


AJAX Suggest Example
AJAX Suggest °¸Àý

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.
ÔÚÏÂÃæµÄAJAX°¸ÀýÖУ¬ÎÒÃǽ«ÑÝʾÓû§ÊÇÈçºÎͨ¹ýÏò±ê×¼µÄHTML±íµ¥ÖÐÊäÈëÊý¾Ý´Ó¶øÊµÏÖ±íµ¥Ò³ÓëWeb·þÎñÆ÷µÄÔÚÏß¹µÍ¨¡£


Type a Name in the Box Below
ÔÚÏÂÃæµÄÊäÈë¿òÄÚÊäÈëÒ»¸öÃû³Æ

First Name:

Suggestions:


Example Explained - The HTML Form
°¸ÀýÆÊÎö —— HTML ±íµ¥

The form above has the following HTML code:
ÉÏÊö±íµ¥°üº¬ÁËÏÂÃæµÄHTML´úÂ룺

<form> 
First Name:
<input type="text" id="txt1"

onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>

As you can see it is just a simple HTML form with an input field called "txt1".
Äã»á·¢ÏÖ£¬ËüÊÇÒ»Õżòµ¥µÄ°üº¬ÐÅÏ¢ÊäÈë¿òµÄHTML±íµ¥£¬ÎļþÃûΪ“txt1”¡£

An event attribute for the input field defines a function to be triggered by the onkeyup event.
ÊäÈë¿òµÄevent [ ʼþ ] ÊôÐÔ¶¨ÒåÁËÒ»¸öÓÉ"onkeyup"ʼþ¼¤·¢µÄº¯Êý¡£

The paragraph below the form contains a span called "txtHint". The span is used as a placeholder for data retrieved from the web server.
±íµ¥Ï·½µÄ¶ÎÂäÖаüº¬ÁËidÃûΪ“txtHint”µÄ“span”ÔªËØ£¬spanÊÇÓÃÓÚ´Ó·þÎñÆ÷¶ËÖØÐ»ñÈ¡Êý¾ÝµÄռλ·û¡£

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.
µ±Óû§ÊäÈëÐÅϢʱ£¬¾Í»áÖ´ÐÐÒ»¸öÃûΪ“showHint()”µÄº¯Êý¡£º¯ÊýµÄÖ´ÐÐÊÇÓÉ"onkeyup"ʼþ¼¤·¢µÄ£»»»¾ä»°Ëµ£ºµ±Óû§µÄÊÖÖ¸Ô¶ÀëÊäÈëÓòµÄ¼üÅ̼üʱ£¬º¯Êý“showHint()”½«»á±»¼¤·¢¡£


Example Explained - The showHint() Function
°¸ÀýÆÊÎö——showHint() º¯Êý

The showHint() function is a very simple JavaScript function placed in the <head> section of the HTML page.
showHint() º¯ÊýÊÇÒ»¸ö·Ç³£¼òµ¥µÄJavaScript º¯Êý£¬¸Ãº¯ÊýÔÚHTMLÒ³ÃæµÄ<head>Ƭ¶ÏÖÐÊéд¡£

The function contains the following code:
¸Ãº¯Êý°üº¬ÁËÏÂÊô´úÂ룺

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);
}

The function executes every time a character is entered in the input field.
ÿµ±Ò»¸ö×Ö·ûÔÚÊäÈëÓòÖÐÊäÈëʱ£¬¾Í»áÖ´ÐÐÕâ¸öº¯Êý¡£

If there is some input in the text field (str.length > 0) the function executes the following:
Èç¹ûÔÚÎı¾Óò£¨str.length > 0£©ÖÐÊäÈëÁËÈô¸ÉÐÅÏ¢ÄÚÈÝ£¬º¯Êý¾Í»áÖ´ÐÐÏÂÃæµÄÄÚÈÝ£º

  • Defines the url (filename) to send to the server
    ¶¨Òå·¢Ë͵½·þÎñÆ÷µÄurl£¨ÎļþÃû£©
  • Adds a parameter (q) to the url with the content of the input field
    ½«¸½´øÊäÈëÓòÄÚÈݵIJÎÊýÌí¼ÓÖÁurl
  • Adds a random number to prevent the server from using a cached file
    Ìí¼ÓÒ»¸öËæ»úÊý×Ö×èÖ¹·þÎñÆ÷ʹÓûº´æÎļþ
  • Creates an XMLHTTP object, and tells the object to execute a function called
    stateChanged when a change is triggered
    ´´½¨Ò»¸öXMLHTTP¶ÔÏó£¬È»ºó£¬µ±Ò»¸ö¸Ä±ä±»¼¤·¢Ê±£¬Î¯ÅɸöÔÏóÖ´ÐÐÒ»¸öÃûΪ“ stateChanged”µÄº¯Êý
  • Opens the XMLHTTP object with the given url.
    ʹÓøø¶¨µÄurl´ò¿ªXMLHTTP¶ÔÏó
  • Sends an HTTP request to the server
    Ïò·þÎñÆ÷·¢ËÍÒ»¸öHTTPÇëÇó

If the input field is empty, the function simply clears the content of the txtHint placeholder.
Èç¹ûÊäÈëÓòΪ¿Õ£¬¸Ãº¯Êý¾Í»áÍêÈ«Çå³ýtxtHintռλ·ûµÄÄÚÈÝ¡£


Example Explained - The GetXmlHttpObject() Function
°¸ÀýÆÊÎö——GetXMLHttpObject() º¯Êý

The example above calls a function called GetXmlHttpObject().
ÉÏÊö°¸ÀýÇëÇóÁËÒ»¸öÃûΪ“GetXmlHttpObject()”µÄº¯Êý¡£

The purpose of the function is to solve the problem of creating different XMLHTTP objects for different browsers. 
º¯ÊýµÄÄ¿µÄÊÇΪÁ˽â¾öÔÚ²»Í¬µÄä¯ÀÀÆ÷Öд´½¨²»Í¬XMLHTTP¶ÔÏóµÄÎÊÌâ¡£

The function is listed below:
º¯ÊýÁоÙÈçÏ£º

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

 


Example Explained - The stateChanged() Function
°¸ÀýÆÊÎö——stateChanged() º¯Êý

The stateChanged() function contains the following code:
stateChanged() º¯Êý°üº¬ÁËÏÂÁдúÂ룺

function stateChanged() 
{
if (xmlHttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}

The stateChanged() function executes every time the state of the XMLHTTP object changes.
ÿµ±XMLHTTP¶ÔÏó·¢Éú¸ü¸Äʱ£¬½«Ö´ÐÐstateChanged() º¯Êý¡£

When the state changes to 4 ("complete"), the content of the txtHint placeholder is filled with the response text.
µ±Çé¿ö¸Ä±äΪ4£¨“Íê³É”£©Ê±£¬txtHint ռλ·ûÖаüº¬ÁËÏìÓ¦Îı¾µÄÄÚÈÝ¡£

ÆÀÂÛ (0) All