日期:2014-05-16 浏览次数:20757 次
/******************************************************************************* * * ajax提交form * ******************************************************************************/ /* 调用示例,在看不懂也没办法了 $(function(){ var tempHTML = '<input type="text" id="aaa" value/><input name="file1" type="file" />'+ "<input type=\"button\" onclick=\"ajaxSub\" value=\"update\">"+ '<div id="output2"></div>'; createForm('asdfe','dddd',tempHTML,'checkddd()'); initParam("/ajaxmodel/default/upload"); createForm('asdfe1','aaaaaaaaaa',tempHTML,''); initParam("/ajaxmodel/default/upload"); } ) function checkddd(){ var ddasd = $("#aaa").val(); if(ddasd){ return true; }else{ alert('请填写'); return false; } } */ /* * <div id="output2"></div> * <form method="post" id="educationalForm" name="educationalForm"> <input type="file" class="fileimg" id="educational" name="educational" title="请上传您的学历证明" default='uploadImg' onChange="Javascript:checkpic(this)"/> </form> <input type="hidden" id="educationalHi" name="educationalHi" /> * * <script> $(function(){ initParamForm("<{$BASE_URL}>ajaxmodel/default/upload","educationalForm"); initParamForm("<{$BASE_URL}>ajaxmodel/default/upload","stationForm"); initParamForm("<{$BASE_URL}>ajaxmodel/default/upload","physicianForm"); initParamForm("<{$BASE_URL}>ajaxmodel/default/upload","professionalForm"); initParamForm("<{$BASE_URL}>ajaxmodel/default/upload","identificationForm"); }) function checkpic(obj){ var formid = obj.name+"Form"; var hid = obj.name+"Hi"; fileObj = obj; ajaxSub(formid,""); } </script> * */ //file控件的对象 var fileObj = null; // 动态创建的formid var yuanpengform = ""; //上传成功后返回的图片ID 隐藏表单的ID var ypimghhid = ""; /** * 初始化参数 * * subUrl 提交的action,需要返回的图片ID * */ function initParam(subUrl,imghidden) { ypimghhid = imghidden; var options = { target : '#output2', beforeSubmit : showRequest, url : subUrl, success : showResponse }; $('#' + yuanpengform).submit(function() { $(this).ajaxSubmit(options); return false;// 阻止表单提交 }); } /** * 初始化参数 手动创建form * * subUrl 提交的action,from id,需要返回的图片ID * */ function initParamForm(subUrl,formid) { var options = { target : '#output2', beforeSubmit : showRequest, url : subUrl, success : showResponse }; $('#' + formid).submit(function() { $(this).ajaxSubmit(options); return false;// 阻止表单提交 }); } /** * * 提交表单 * */ function ajaxSub(formid, method) { var flag = true; if (method != '') { if (!eval(method)) { flag = false; } } if (flag) { $('#' + formid).submit(); } } /** * * 回调函数 ajax请求时会调用此方法 form的数据,jqForm jquery表单对象,options 初始化的参数 * */ function showRequest(formData, jqForm, options) { waitting(); var queryString = $.param(formData); return true; } /** * * 回调函数 ajax提交成功后调用 * * responseText 返回输出的结果,statusText 执行是否成功 * */ function showResponse(responseText, statusText,xhr) { removeAll(); if(showError(error)){ var error = $("#uploaderror").html(); if(error == "no"){ alert("上传失败,您选择的图片大小或者类型不符合要求!"); cleanFile(fileObj.name); var objhidden = $("input[name='"+fileObj.name+"Hi']"); objhidden.val(""); }else{ //alert("恭喜您,上传成功!"); var objhidden = $("input[name='"+fileObj.name+"Hi']"); objhidden.val(error); } } } function showError(error){ return true; } function checkPicResponse(responseText, statusText,xhr) { try{ var error = $("#uploaderror").html(); var jsonss = jQuery.parseJSON(error); var flag = true; var filename = ""; if(jsonss != null){ for(var i=0;i<jsonss.length;i++){ if(jsonss[i][1] != 1){ flag = false; filena