w3pop.com :: ÍøÂçѧԺ :: AJAX :: AJAX Suggest °¸Àý
We have seen that AJAX can be used to create more interactive applications.
ÎÒÃÇÒѾ·¢ÏÖ£¬AJAX¿ÉÒÔÓÃÓÚ´´½¨¸ü¾ß½»»¥ÐÔµÄÓ¦ÓóÌÐò¡£
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·þÎñÆ÷µÄÔÚÏß¹µÍ¨¡£
Suggestions:
The form above has the following HTML code:
ÉÏÊö±íµ¥°üº¬ÁËÏÂÃæµÄHTML´úÂ룺
<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()”½«»á±»¼¤·¢¡£
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) |
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£©ÖÐÊäÈëÁËÈô¸ÉÐÅÏ¢ÄÚÈÝ£¬º¯Êý¾Í»áÖ´ÐÐÏÂÃæµÄÄÚÈÝ£º
If the input field is empty, the function simply clears the content of the txtHint placeholder.
Èç¹ûÊäÈëÓòΪ¿Õ£¬¸Ãº¯Êý¾Í»áÍêÈ«Çå³ýtxtHintռλ·ûµÄÄÚÈÝ¡£
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() |
The stateChanged() function contains the following code:
stateChanged() º¯Êý°üº¬ÁËÏÂÁдúÂ룺
function stateChanged() |
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