动态显示文本框的字符串数(ajax入门小程序) 

本文介绍了一个使用纯Ajax技术实现的小程序,该程序能够实时显示用户输入字符串的长度。通过客户端JavaScript与服务器端交互,无需刷新页面即可更新字符串长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


首先添加一个html页其页面代码如下:
<html>
<head>
<title>在ASP.NET的第一个Ajax小程序</title>
<link rel="stylesheet" type="text/css" href="../css/main.css"/>
<script language="javascript">
        
var xmlHttp
        
function showHint(str)
        
{
            
if (str.length > 0)
            
{
                
var url="GetRequest.aspx?name=" + str
                
if (window.ActiveXObject) 
                    xmlHttp 
= new ActiveXObject("Microsoft.XMLHTTP"); 
                }

                  xmlHttp.onreadystatechange
=stateChanged
                xmlHttp.open(
"POST", url , true)
                xmlHttp.send(
null)
            }

            
else
            
{
                document.getElementById(
"txtHint").innerHTML=""
            }

        }

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

        }


</script>
</head><body>
<div class=content>
<h1>纯Ajax实现的小程序</h1>
<form>
    
<p>输入一个字符串:<input type="text" id="txt1"onkeyup="showHint(this.value)"></p>

<p>
    字符串的长度: 
<span id="txtHint"></span></p> 
   
</form> </div></body>
</html>


述代码中GetRequest.aspx?name=" + str 指明将请求页面GetRequest.aspx并把用户输入的字符串传递给页面GetRequest.aspx,所以下一步就是建立GetRequest.aspx页面计算字符串长度,后台代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class PureAjax_GetRequest : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (Request.QueryString["name"!= "")
            Response.Write(Request.QueryString[
"name"].Length);
        
else
            Response.Write(
"NULL");
    }

}

转载于:https://www.cnblogs.com/hya1109/archive/2007/09/11/889198.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值