编程论坛's Archiver

huangwanfu 发表于 2007-9-19 16:24

[求助]ajax实例的一个问题

<P>请问这种情况怎么处理啊<BR>在getInfo()使用responseText中反馈回查询数据.<BR>在下面使用<BR>function check() <BR>{getInfo();<BR>var alltxt=allcontent;<BR>......<BR>}<BR>但是check中只能使用上一次getInfo()查询出来的数据<BR>应该怎么处理才能使用本次onkeyup触发后反馈的查询数据啊<BR><BR>原码如下:<BR>&lt;%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %&gt;<BR>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<a href="http://www.w3.org/TR/html4/loose.dtd" target="_blank" >http://www.w3.org/TR/html4/loose.dtd</A>"&gt;<BR>&lt;html&gt;<BR>&lt;head&gt;<BR>&lt;link href="./images/css/body.css" rel="stylesheet" type="text/css"&gt;<BR>&lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312"&gt;<BR>&lt;title&gt;病虫害查询&lt;/title&gt;<BR>&lt;script language="javascript" &gt;<BR>var httprequest;<BR>var allcontent;<BR>function createRequest()<BR>{<BR>    try{<BR>        httprequest=new XMLHttpRequest();<BR>    }catch(trymicrosoft)<BR>    {<BR>        try{<BR>            httprequest=new ActiveXObject("Msxml2.XMLHTTP");<BR>        }catch(othermicrosoft)<BR>        {<BR>            try{<BR>                httprequest=new ActiveXObject("Microsoft.XMLHTTP");<BR>            }<BR>            catch(failed)<BR>            {<BR>                httprequest=false;<BR>            }<BR>        }<BR>    }<BR>    if(!httprequest)<BR>    {<BR>       alert("err Happend!");<BR>       return null;<BR>  }        <BR>    return httprequest;<BR>}<BR>function getInfo()<BR>{   createRequest();<BR>    var username=document.getElementById("name").value;<BR>    if(username!="")<BR>    {var url='answer.jsp?name='+username;<BR>     httprequest.open("GET",url,"true");<BR>     httprequest.onreadystatechange=disResult;//隐性的循环<BR>     httprequest.send(null);<BR>     }<BR>}<BR>function disResult()<BR>{<BR>    if(httprequest.readystate==4)<BR>    {<BR>        if(httprequest.status==200)<BR>        {<BR>            allcontent = httprequest.responseText;<BR>          }<BR>        else<BR>        {<BR>            alert('Something Wrong has Happend!'+httprequest.status);<BR> }<BR>    }<BR>return allcontent;    <BR>}<BR>function wait()<BR>{}<BR>function checkword()<BR>{<BR>allcontent="ssd";<BR>getInfo();<BR>var wordvalue=document.getElementById("name").value.toLowerCase();<BR>var alltxt=allcontent;<BR>var alltxtpp=alltxt.toLowerCase();<BR>var alltxt_xiang=alltxt.split("|");<BR>var alltxt_xiang1=alltxtpp.split("|");<BR>var inhtml="&lt;ul&gt;"<BR>var isyou=0;<BR>for (i=0;i&lt;alltxt_xiang1.length;i++)<BR>{<BR>if(alltxt_xiang1[i].substr(0,1)!="")<BR>{<BR>inhtml=inhtml+"&lt;li onclick=\"document.getElementById('name').value=this.innerHTML;document.getElementById('showmenu').style.display='none';\" onmouseover=\"this.style.backgroundColor='#666666'\" onmouseout=\"this.style.backgroundColor=''\"&gt;"+alltxt_xiang[i]+"&lt;/li&gt;";<BR>isyou=1;}<BR>}<BR>inhtml=inhtml+"&lt;/ul&gt;";<BR>if (isyou==1)<BR>{<BR>document.getElementById("showmenu").innerHTML=inhtml;<BR>document.getElementById("showmenu").style.display="";<BR>}<BR>else<BR>{<BR>document.getElementById("showmenu").innerHTML="";<BR>document.getElementById("showmenu").style.display="none";<BR>}<BR>if (wordvalue=="")<BR>{<BR>document.getElementById("showmenu").innerHTML="";<BR>document.getElementById("showmenu").style.display="none";<BR>}<BR>}<BR>&lt;/script&gt;<BR>&lt;/head&gt;<BR>&lt;body&gt;<BR>&lt;form name="chaxun" method="post" action=""&gt;<BR>&lt;input  name="label" type="button" value="虫害名称"&gt;&lt;/input&gt;<BR>&lt;input  name="name" type="text" style="" id="name"  onkeyup="checkword();" onblur="checkword();"  autocomplete = "off" value=""&gt;&lt;/input&gt;<BR>&lt;div style="position: absolute; width: 200px; height: 100px; z-index: 1; left: 10px; top: 40px;border:1px solid #666666;display:none; font-size:14px" id="showmenu"&gt;&lt;/div&gt;<BR>&lt;input  type="submit"  value="提交" style=""&gt;&lt;/input&gt;<BR>&lt;input  type="reset"  value="取消" style=""&gt;&lt;/input&gt;<BR>&lt;input  name="name" type="button" value="帮助"&gt;&lt;/input&gt;<BR>&lt;form name="chaxun" method="post" action=""&gt;<BR>&lt;br&gt;<BR>&lt;input  name="d" type="button" value="病害名称"&gt;&lt;/input&gt;<BR>&lt;input  name="df" type="text" style=""&gt;&lt;/input&gt;<BR>&lt;input  type="submit"  value="提交" style=""&gt;&lt;/input&gt;<BR>&lt;input  type="reset"  value="取消" style=""&gt;&lt;/input&gt;<BR>&lt;/form&gt;<BR>&lt;/body&gt;<BR>&lt;/html&gt;<BR><BR>请求页answer.jsp执行数据库查询并按照 "士大夫的|你好|不|" 的格式返回数据.<BR><BR>遇到问题<BR>例如:输入 "你" 第一次未定义的allcontent,<BR>再输入 "褐" 会反馈回第一个字"你"查询出来的结果,<BR>同理 再输入 "病"反馈回第二个字"褐"查询出来的结果,<BR>如果重复输入,反馈没有问题,估计是从IE temp文件中读取的内容.<BR><BR></P>

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.