//这是公共的ajax提交文件 //@lichaoyong 2013-8-30 var parseParam=function(param, key){ var paramStr=""; if(param instanceof String||param instanceof Number||param instanceof Boolean){ paramStr+="&"+key+"="+encodeURIComponent(param); }else{ $.each(param,function(i){ var k=key==null?i:key+(param instanceof Array?"["+i+"]":"."+i); paramStr+='&'+parseParam(this, k); }); } return paramStr.substr(1); }; var lock = false; var timeout = 2000; var forwordurl =''; var normalajax = { success:false, send:function(param){ //param.formid = ""; form表单的id号 //判断是否为form节点 if(typeof param != "object"||typeof param.url == "undefined"){ return false; } if(typeof param.forword != "undefined"){ forwordurl = typeof param.forword.url == "undefined"?'':param.forword.url; typeof param.forword.timeout == "number"? timeout = param.forword.timeout:2000; } if(typeof param.formid != "undefined"){ var objb = $("#"+param.formid)[0]; if(objb.nodeName=="FORM"){//如果是form表单 if(typeof param.formid == "undefined"){ return false; } var paramstr = $("#"+param.formid).serialize(); if(typeof param.obj == "object"){ paramstr+="&"+parseParam(param.obj); } if(!lock){ var bool = this.senddata(param.url,paramstr,timeout,forwordurl); } } }else{//否则是div if(typeof param.obj != "object"){ return false; } if(!lock){ //当一个请求完成之后再去请求下一个 this.senddata(param.url,param.obj,timeout,forwordurl); } } }, senddata:function(url,obj,timeout,forwordurl){ lock = true; $.post(url,obj,function(result){ lock = false; if(result.state){ typeof common_zf_obj == "undefined"?'':common_zf_obj.setUnload(true); } dialog.finish({ content:result.msg, success:result.state, timeout:timeout, callback:function(){ typeof result.url == "undefined"?'':location.href=result.url; } }); if(forwordurl){ setTimeout(function(){ location.href = forwordurl; },timeout); } },'json'); } }; 对于form表单的使用方式 //formid表示from表单form标签id号 //url表示action路径 forword表示成功之后跳转路径,已经停留时间normalajax.send({formid:'form1',url:'/test.php',forword:{url:'/admin/user/list',timeout:2000}});对于普通div标签的使用方式
url表示action路径
obj表示提交的对象,参数
formword表示成功之后跳转路径,已经停留时间
normalajax.send({url:'/test.php',obj:{'id':id,'answer_content':inputnode},forword:{'url':location.href,'timeout':2000}});
jquery写的ajax提交,快捷方便使用
最新推荐文章于 2024-01-02 21:16:13 发布