function mySubmit(theForm,url,result){ function default_callback(res){ result.html(res.info); if(res.status=='1'){ if(res.url!=''){ location.href = res.url; }else{ window.setTimeout(function(){ result.html(''); document.getElementById(theForm).reset(); },2000); } } } if(url && typeof(url)=='string'){ var callback = arguments[3] || default_callback; }else{ var callback = arguments[1] || default_callback; url = $('#'+theForm).prop('action'); } if(result){ var result = $('#'+result); }else if($('#result').length>0){ result = $('#result'); }else{ result = $("<font class='tip' color='red' id='result'></font>"); $('#'+theForm).append(result); } result.html('处理中……'); var formdata = null; try{ formdata = new FormData(); }catch(ex){} var filedata = $('#'+theForm+' input[type="file"]'); if(filedata.length>0 && !formdata){ alert('你的浏览器版本太低,请升级浏览器或使用其他浏览器(例如:chrome浏览器)!'); return false; } filedata.each(function(){ var _this = this; if(_this.files.length==1){//单文件上传 var file = _this.files[0]; formdata.append(_this.name,file); }else if(_this.files.length>1){//多文件上传 for (var i=0,len=_this.files.length; i<len; i++) { var file = _this.files[i]; formdata.append(_this.name+'['+i+']',file); }; } }); if(!formdata){ formdata = $('#'+theForm).serialize(); }else{ var data = $('#'+theForm).serializeArray(); for(var i=0,len=data.length; i<len; i++){ formdata.append(data[i]['name'], data[i]['value']); } } $.ajax({ url:url, type:'post', data:formdata, processData:false, contentType:false, dataType:'json', success:callback }); return false; }