其他-javascript获取表单的信息并用ajax提交

function Pub(){  } 
Pub.newXMLHttpRequest=function newXMLHttpRequest() {
	  var xmlreq = false;
	  if (window.XMLHttpRequest) {
	  
	    xmlreq = new XMLHttpRequest();
	  } else if (window.ActiveXObject) {
	     
	    try {
	      
	      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e1) {
	       
	      try {
	      
	        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
	      } catch (e2) {
	          
	        alert(e2);
	      }
	    }
	  }
	  return xmlreq;
	}
Pub.getParams2Str=function getParams2Str(sForm){

	 var strDiv="";
	      
	 try {
	    var objForm=document.forms[sForm];
	  if (!objForm)
	    return strDiv;
	  var elt,sName,sValue;
	  for (var fld = 0; fld < objForm.elements.length; fld++) {
	      elt = objForm.elements[fld];    
	      sName=elt.name;
	      sValue=""+elt.value;
	      if(fld==objForm.elements.length-1)
	          strDiv=strDiv + sName+"="+sValue+"";
	       else   
	          strDiv=strDiv + sName+"="+sValue+"&";
	   }


	  }
	  catch (ex) {
	    return strDiv;
		}
	 
	   return strDiv;
	}
Pub.submitActionWithForm=function(action,sForm){
  var req = Pub.newXMLHttpRequest();
  req.open("POST", action, false);
  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
  var str = Pub.getParams2Str(sForm); 
 
  req.send(str);

}

你可能感兴趣的:(ajax)