日期:2014-05-18  浏览次数:20765 次

如何使表单异步提交呢?
如题

------解决方案--------------------
function Ajax(method,url,param,ft,fun)
{
this.xhr=this.getxhr();
this.docType=ft;
switch(method.toLowerCase())
{
case "post":this.post(url,param);break;
case "get":this.get(url,param);break;
}
this._readystate=null;
this.fun=fun;

}

Ajax.prototype.getxhr=function()
{
var ms=['Microsoft.XMLHttp','MSXML.XMLHttp','MSXML2.XMLHttp','MSXML3.XMLHttp'];
if(window.ActiveXObject)
{
try{
for(var i=0;i<ms.length;i++)
{
return new ActiveXObject(ms[i]);
}
}
catch(e){}
}
else if(window.XMLHttpRequest)
{
var req=new XMLHttpRequest();

if(req.overrideMimeType)
{
req.overrideMimeType("text/xml");
}

}
return req;
}

Ajax.prototype.post=function(url,param)
{
var rc=Math.round(Math.random()*999);
if(url.indexOf("?")>=0)
{
url+="&arnd="+rc;
}
else
{
url+="?arnd="+rc;
}
this.xhr.open("post",url,true);
var obj=this;
this.xhr.onreadystatechange=function(){obj.call_(obj)}
this.xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
this.xhr.send(param);
}

Ajax.prototype.get=function(url,param)
{
var rc=Math.round((Math.random()*999));
if(url.indexOf("?")>=0)
{
url+="&"+param+"&arnd="+rc;
}
else
{
url+="?"+param+"&arnd="+rc;
}
this.xhr.open("get",url,true);
var obj=this;
this.xhr.onreadystatechange=function(){obj.call_(obj)}
this.xhr.send(null);
}

Ajax.prototype.call_=function(req)
{
if(req.xhr.readystate==4)
{
if(req.xhr.status==200)
{
if(req.docType.toLowerCase()=="xml")
{
eval(req.fun+"(req.xhr.responseText)");
}
else
{
eval(req.fun+"(req.xhr.responseText)");
}
}
}
}

------解决方案--------------------
用Ajax
prototype 就很好
------解决方案--------------------
just easy
 楼上说的好
给你个样例
<script type="text/javascript" src="../js/prototype/prototype.js"></script>
new Ajax.Updater('content',
url,//特别说明,如下
{method:'get',
evalScripts:true,
onFailure:function(){programing = false;},
onSuccess:function(){programing = false;}
});
url='xx.do?'+$('formID').serialize();
<form id='formID'></form>
里面你想放啥就放啥咯~
------解决方案--------------------
prototype
DWR
这两个AJAX框架,都可以实现你的要求