jQuery.fn.ajaxSubmit - 使用 AJAX 提交 HTML 表单
var feature = {}; feature.fileapi = $('').get(0).files !== undefined; feature.formdata = window.FormData !== undefined;
var hasProp = !!$.fn.prop;
// attr2 uses prop when it can but checks the return type for // an expected string. this accounts for the case where a form // contains inputs with names like 'action' or 'method'; in those // cases 'prop' returns the element $.fn.attr2 = function() { if ( ! hasProp ) { return this.attr.apply(this, arguments); } var val = this.prop.apply(this, arguments); if ( ( val && val.jquery ) || typeof val === 'string' ) { return val; } return this.attr.apply(this, arguments); };
/**
-
ajaxSubmit() provides a mechanism for immediately submitting
-
an HTML form using AJAX. */ $.fn.ajaxSubmit = function(options) { /*jshint scripturl:true */
// fast fail if nothing selected (http://dev.jquery.com/ticket/2752) 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 }; } else if ( options === undefined ) { options = {}; }
method = options.type || this.attr2('method'); action = options.url || this.attr2('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 || $.ajaxSettings.type, 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; }
原文地址: https://www.cveoy.top/t/topic/mFfJ 著作权归作者所有。请勿转载和采集!