当前位置: 首页 > 网络学院 > 客户端脚本教程 > 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 占位符中包含了响应文本的内容。