//20170316更新 增加文件下载时再刷新功能 //20170405 fileChange 增加 ftype 参数 //20191010修改了支持Pall 提交 ; (function($) { "use strict"; var feature = {}; feature.fileapi = $("").get(0).files !== undefined; feature.formdata = window.FormData !== undefined; $.fn.ajaxSubmit = function(options) { if (!this.length) { log('ajaxSubmit: skipping submit process - no element selected'); return this; } var method, action, url, $form = this; if (typeof options == 'function') { options = { success: options }; } method = this.attr('method'); action = this.attr('action'); //action = 'Post.ashx?query=' + encodeURIComponent(action.substring(action.indexOf('=') + 1, action.length)); //修改了支持Pall 提交 if (action.indexOf('Pall.ashx')===-1) { action = 'Post.ashx?query=' + encodeURIComponent(action.substring(action.indexOf('=') + 1, action.length)); } else{ action = 'Pall.ashx?query=' + encodeURIComponent(action.substring(action.indexOf('=') + 1, action.length)); } // alert(action); url = (typeof action === 'string') ? $.trim(action) : ''; url = url || window.location.href || ''; if (url) { // clean url (don't include hash vaue) url = (url.match(/^([^#]+)/) || [])[1]; } options = $.extend(true, { url: url, success: $.ajaxSettings.success, type: method || 'GET', iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank' }, options); // hook for manipulating the form data before it is extracted; // convenient for use with rich editors like tinyMCE or FCKEditor var veto = {}; this.trigger('form-pre-serialize', [this, options, veto]); if (veto.veto) { log('ajaxSubmit: submit vetoed via form-pre-serialize trigger'); return this; } // provide opportunity to alter form data before it is serialized if (options.beforeSerialize && options.beforeSerialize(this, options) === false) { log('ajaxSubmit: submit aborted via beforeSerialize callback'); return this; } var traditional = options.traditional; if (traditional === undefined) { traditional = $.ajaxSettings.traditional; } var elements = []; var qx, a = this.formToArray(options.semantic, elements); if (options.data) { options.extraData = options.data; qx = $.param(options.data, traditional); } // give pre-submit callback an opportunity to abort the submit if (options.beforeSubmit && options.beforeSubmit(a, this, options) === false) { log('ajaxSubmit: submit aborted via beforeSubmit callback'); return this; } // fire vetoable 'validate' event this.trigger('form-submit-validate', [a, this, options, veto]); if (veto.veto) { log('ajaxSubmit: submit vetoed via form-submit-validate trigger'); return this; } var q = $.param(a, traditional); if (qx) { q = (q ? (q + '&' + qx) : qx); } if (options.type.toUpperCase() == 'GET') { options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q; options.data = null; // data is null for 'get' } else { options.data = q; // data is the query string for 'post' } var callbacks = []; if (options.resetForm) { callbacks.push(function() { $form.resetForm(); }); } if (options.clearForm) { callbacks.push(function() { $form.clearForm(options.includeHidden); }); } // perform a load on the target only if dataType is not provided if (!options.dataType && options.target) { var oldSuccess = options.success || function() { }; callbacks.push(function(data) { var fn = options.replaceTarget ? 'replaceWith' : 'html'; $(options.target)[fn](data).each(oldSuccess, arguments); }); } else if (options.success) { callbacks.push(options.success); } options.success = function(data, status, xhr) { // jQuery 1.4+ passes xhr as 3rd arg var context = options.context || this; // jQuery 1.4+ supports scope context for (var i = 0, max = callbacks.length; i < max; i++) { callbacks[i].apply(context, [data, status, xhr || $form, $form]); } }; // are there files to upload? // [value] (issue #113), also see comment: // https://github.com/malsup/form/commit/588306aedba1de01388032d5f42a60159eea9228#commitcomment-2180219 var fileInputs = $('input[type=file]:enabled[value!=""]', this); var hasFileInputs = fileInputs.length > 0; var mp = 'multipart/form-data'; var multipart = ($form.attr('enctype') == mp || $form.attr('encoding') == mp); var fileAPI = feature.fileapi && feature.formdata; log("fileAPI :" + fileAPI); var shouldUseFrame = (hasFileInputs || multipart) && !fileAPI; var jqxhr; // options.iframe allows user to force iframe mode // 06-NOV-09: now defaulting to iframe mode if file input is detected if (options.iframe !== false && (options.iframe || shouldUseFrame)) { // hack to fix Safari hang (thanks to Tim Molendijk for this) // see: http://groups.google.com/group/jquery-dev/browse_thread/thread/36395b7ab510dd5d if (options.closeKeepAlive) { $.get(options.closeKeepAlive, function() { jqxhr = fileUploadIframe(a); }); } else { jqxhr = fileUploadIframe(a); } } else if ((hasFileInputs || multipart) && fileAPI) { jqxhr = fileUploadXhr(a); } else { jqxhr = $.ajax(options); } $form.removeData('jqxhr').data('jqxhr', jqxhr); // clear element array for (var k = 0; k < elements.length; k++) elements[k] = null; // fire 'notify' event this.trigger('form-submit-notify', [this, options]); return this; // utility fn for deep serialization function deepSerialize(extraData) { var serialized = $.param(extraData).split('&'); var len = serialized.length; var result = {}; var i, part; for (i = 0; i < len; i++) { part = serialized[i].split('='); result[decodeURIComponent(part[0])] = decodeURIComponent(part[1]); } return result; } // XMLHttpRequest Level 2 file uploads (big hat tip to francois2metz) function fileUploadXhr(a) { var formdata = new FormData(); for (var i = 0; i < a.length; i++) { formdata.append(a[i].name, a[i].value); } if (options.extraData) { var serializedData = deepSerialize(options.extraData); for (var p in serializedData) if (serializedData.hasOwnProperty(p)) formdata.append(p, serializedData[p]); } options.data = null; var s = $.extend(true, {}, $.ajaxSettings, options, { contentType: false, processData: false, cache: false, type: method || 'POST' }); if (options.uploadProgress) { // workaround because jqXHR does not expose upload property s.xhr = function() { var xhr = jQuery.ajaxSettings.xhr(); if (xhr.upload) { xhr.upload.onprogress = function(event) { var percent = 0; var position = event.loaded || event.position; /*event.position is deprecated*/ var total = event.total; if (event.lengthComputable) { percent = Math.ceil(position / total * 100); } options.uploadProgress(event, position, total, percent); }; } return xhr; }; } s.data = null; var beforeSend = s.beforeSend; s.beforeSend = function(xhr, o) { o.data = formdata; if (beforeSend) beforeSend.call(this, xhr, o); }; return $.ajax(s); } // private function for handling file uploads (hat tip to YAHOO!) function fileUploadIframe(a) { var form = $form[0], el, i, s, g, id, $io, io, xhr, sub, n, timedOut, timeoutHandle; var useProp = !!$.fn.prop; var deferred = $.Deferred(); if ($('[name=submit],[id=submit]', form).length) { // if there is an input with a name or id of 'submit' then we won't be // able to invoke the submit fn on the form (at least not x-browser) alert('Error: Form elements must not have name or id of "submit".'); deferred.reject(); return deferred; } if (a) { // ensure that every serialized input is still enabled for (i = 0; i < elements.length; i++) { el = $(elements[i]); if (useProp) el.prop('disabled', false); else el.removeAttr('disabled'); } } s = $.extend(true, {}, $.ajaxSettings, options); s.context = s.context || s; id = 'jqFormIO' + (new Date().getTime()); if (s.iframeTarget) { $io = $(s.iframeTarget); n = $io.attr('name'); if (!n) $io.attr('name', id); else id = n; } else { $io = $('"); if (!(formData.ReUrl == "" || formData.ReUrl == "null" || formData.ReUrl == "undefined" || formData.ReUrl == null)) { location.href = formData.ReUrl; } //window.open("post.ashx?filedown="+formData.Url.replace("report/","")); //location.href = formData.Url; } else if (formData.Type == "syserror") { alert(formData.Message); } else if (formData.Type == "error") { if (formData.Message != "") alert(formData.Message); if (formData.Rid != "") { $('#' + formData.Rid).html(formData.Rcontent.replace(//g, ">")); } else { if (formData.Url != "") location.href = formData.Url; } } else if (formData.Type != "file") { //alert(1); location.href = formData.Url; } $("#sysstatus").hide(); $("#poststatus").dialog("close"); return true; } function DrawImage(ImgD, iwidth, iheight) { var image = new Image(); image.src = ImgD.src; if (image.width > 0 && image.height > 0) { flag = true; if (image.width < 20) { ImgD.width = 20; ImgD.height = (image.height * 20) / image.width; } else if (image.width / image.height >= iwidth / iheight) { if (image.width > iwidth) { ImgD.width = iwidth; ImgD.height = (image.height * iwidth) / image.width; } else { ImgD.width = image.width; ImgD.height = image.height; } } else { if (image.height > iheight) { ImgD.height = iheight; ImgD.width = (image.width * iheight) / image.height; } else { ImgD.width = image.width; ImgD.height = image.height; } } } } function update(url) { url = encodeURI(url); $("#sysstatus").show(); $.getJSON(url, function(data) { // alert(data.r1 + "\n" + data.r2); if (data.r1 == "syserror") { ; } else if (data.r1 == "error") { alert(data.r2); } else if (data.r1 == "null") { location.href = data.r2; } else { if (data.r2 == "") { alert(data.r1); } else { alert(data.r1); location.href = data.r2; } } $("#sysstatus").hide(); } ); } function ckchange(obj) { var f = obj.checked; var chkColor = "#fdfdd3"; //选中后颜色 var noColor = "#fff"; //取消选中后的颜色 if (f) obj.parentElement.parentElement.style.backgroundColor = chkColor; else obj.parentElement.parentElement.style.backgroundColor = noColor; } function bmouseout(x) { $(x).attr("class", "genButton"); } function bmouseover(x) { $(x).attr("class", "genButtonover"); } function gridcheckall(x) { var s = $(x).attr("checked"); if (s == "checked") $(".gridcheck").attr("checked", "checked"); else $(".gridcheck").attr("checked", false); $(".gridcheck").each(function() { ckchange(this); }); } var isIE = /msie/i.test(navigator.userAgent) && !window.opera; function fileChange(target, fsize,ftype) { var fileSize = 0; var filetypes = [".jpg", ".png", ".jpeg", ".bmp"]; if (ftype==9) {filetypes = [".xlsx", ".xls"];} //var filetypes = [".jpg", ".png", ".rar", ".txt", ".zip", ".doc", ".ppt", ".xls", ".pdf", ".docx", ".xlsx"]; var filepath = target.value; var filemaxsize = fsize; //1024 * 2; //2M if (filepath) { var isnext = false; var fileend = filepath.substring(filepath.indexOf(".")); if (filetypes && filetypes.length > 0) { for (var i = 0; i < filetypes.length; i++) { if (filetypes[i].toLowerCase() == fileend.toLowerCase()) { isnext = true; break; } } } if (!isnext) { alert("不接受此文件类型!"); $(target).after($(target).clone().val("")); $(target).remove(); return false; } } else { return false; } if (isIE && !target.files) { var filePath = target.value; var fileSystem = new ActiveXObject("Scripting.FileSystemObject"); if (!fileSystem.FileExists(filePath)) { alert("上传文件不存在,请重新输入!"); return false; } var file = fileSystem.GetFile(filePath); fileSize = file.Size; } else { fileSize = target.files[0].size; } var size = fileSize / 1024; if (size > filemaxsize) { alert("上传文件大小不能大于" + filemaxsize + "K!"); $(target).after($(target).clone().val("")); $(target).remove(); return false; } if (size <= 0) { alert("上传文件大小不能为0M!"); $(target).after($(target).clone().val("")); $(target).remove(); return false; } return true; }