当前位置:编程学习 > JS >>

Ajax异步返回(急)

后台里有个str这样的字符穿 比如内容是这样的 str=@4,佛山市@5,广州市@6,深圳市@7,珠海市@8,汕头市@9,东莞市

 

就是想用ajax做个下拉出来。把str里的“市”都加到selelct里去显示。数字都是获取的id 大家都帮帮忙 本人急

补充:先要把数据重后台获取到喔.. 最好给代码,本人ajax太差了..

追问:不是很明白你的数据是从哪获取到了. 后台输出了这样一个字符串 str

你些的那个 temp 里的数据是怎么来的??

可以再写明白吗 谢谢.

 

答案:不知道你用什么做ajax,这里是js里用xmlHTTP。

<script>
var g = 0
function $(s){return document.getElementById(s);}
function getXmlhttp()
{ var http_request;
  if (window.XMLHttpRequest)
  {  http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType)
     {  http_request.overrideMimeType("text/xml"); }
  }
  else if (window.ActiveXObject)
  {  try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); }
     catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); }
                 catch (e) {} }
  }
  if (!http_request) {  window.alert("can't create XMLHttpRequest object."); return null; } 
  return http_request;
}
var test="@4,佛山市@5,广州市@6,深圳市@7,珠海市@8,汕头市@9,东莞市"  //测试用。
function LoadAJAXInfo(url,strBoxId)
{if (g>0) {return false;}  //测试用,防止多次加入项
 SetOption(test);return;  //测试用,信息放到下拉框中,二不是从服务端读取。
 var oInfoBox=$(strBoxId);
 if (!oInfoBox) { return false;}
  var xhttp=getXmlhttp();
    xhttp.onreadystatechange=function()
    {  if (xhttp.readyState == 4 )
       {
          if (xhttp.status==200 || window.location.href.indexOf("http")==-1)
          {   SetOption(xhttp.responseText);   //这就是从你的url获取到的字符串,然后去设置到下拉框中
          }
          else if (xhttp.status==500)
          {   oInfoBox.innerHTML = xhttp.responseText;   }
          else
          { oInfoBox.innerHTML ="error, no found page info."+xhttp.status;  }
       }
    }
    xhttp.open("GET",url,true);
    xhttp.send(null);
}
function SetOption(temp)
{ var b; var select_obj=$("select1");
  var a = temp.split("@");
  for (i=1;i<a.length;i++)
  { b = a[i].split(",");select_obj.options.add(new Option(b[1],b[0]));  }
  g = 1;
}
</script>
<div style="height:500px;width:100%">
<select id=select1><option value=0>请选择</option></select>
<button onclick="LoadAJAXInfo('这里是你要获取信息的URL,例如getinfo.php或asp等','errDiv');">获取信息</button>
</div>
<div id=errDiv></div>

上一个:ajax问题,紧急求救
下一个:ajax Dwr生成表格代码

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,