[Ajax]AJAX的简单应用(加法运算示例)
2023-09-27 14:29:23 时间
createXMLHttpRequest();
var url = "Handler.ashx?Num1=" + document.getElementById("num1").value + " Num2=" + document.getElementById("num2").value;
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = showResult;
xmlHttp.send(null);
function showResult()
//请求完成
if (xmlHttp.readyState == 4)
if (xmlHttp.status == 200)
document.getElementById("result").value=xmlHttp.responseText;
/script
/head
body
form id="form1" runat="server"
div
input id="num1" type="text" value="0" onkeyup="addNumber();"/ + input id="num2" type="text"
value="0" onkeyup="addNumber();"/ = input id="result" type="text" / /div
/form
/body
/html
蓬莱仙羽 麦子学院讲师,游戏蛮牛专栏作家,CSDN博客专家,热爱游戏开发,热爱Coding!
4.在文本框的属性中添加 onkeyup属性,添加addNumber()函数
5.添加一般事件处理程序
并修改函数
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int result = Convert.ToInt32(context.Request.QueryString["Num1"]) + Convert.ToInt32(context.Request.QueryString["Num2"]);
context.Response.Write(result);
}
6.运行
在文本框中输入一个数,相应的结果就会显示出来
蓬莱仙羽 麦子学院讲师,游戏蛮牛专栏作家,CSDN博客专家,热爱游戏开发,热爱Coding!