JQeury form插件的ajaxForm方法和ajaxSubmit方法的区别

兄弟们哪位知道,JQeury form插件的ajaxForm方法和ajaxSubmit方法的区别
最新回答
梦带我旅行

2024-04-24 05:36:34

  通过代码来举例:

var val = $("#testInput").val();//取值
$.ajax({//异步提交
type: "post",//method
url: "snowcoal.com/api/testSubmit",//uri
data:{val:val},//data transfer
success: function(data){//do sth.
}
});
  1. ajaxForm并不提交表单,而为提交做一些准备工作,比如获取数据、匹配数据的完整性

  2. ajaxSubmit会直接提交表单,两者最主要区别,当然这两个插件都不用也可以异步提交。

消失在我眼中

2024-04-24 09:15:14

摘要
JQuery form ajaxForm ajaxSubmit 必须要有submit元素

/**
* ajaxForm() provides a mechanism for fully automating form submission.
*
* The advantages of using this method instead of ajaxSubmit() are:
*
* 1: This method will include coordinates for <input type="image" /> elements (if the element
* is used to submit the form).
* 2. This method will include the submit element's name/value data (for the element that was
* used to submit the form).
* 3. This method binds the submit() method to the form for you.
*
* The options argument for ajaxForm works exactly as it does for ajaxSubmit. ajaxForm merely
* passes the options argument along after properly binding events for submit elements and
* the form itself.
*/
$.fn.ajaxForm = function(options) {
options = options || {};
options.delegation = options.delegation && $.isFunction($.fn.on);

// in jQuery 1.3+ we can fix mistakes with the ready state
if (!options.delegation && this.length === 0) {
var o = { s: this.selector, c: this.context };
if (!$.isReady && o.s) {
log('DOM not ready, queuing ajaxForm');
$(function() {
$(o.s,o.c).ajaxForm(options);
});
return this;
}
// is your DOM ready? ocs.jquery.com/Tutorials:Introducing_$(document).ready()
log('terminating; zero elements found by selector' + ($.isReady ? '' : ' (DOM not ready)'));
return this;
}

if ( options.delegation ) {
$(document)
.off('submit.form-plugin', this.selector, doAjaxSubmit)
.off('click.form-plugin', this.selector, captureSubmittingElement)
.on('submit.form-plugin', this.selector, options, doAjaxSubmit)
.on('click.form-plugin', this.selector, options, captureSubmittingElement);
return this;
}

return this.ajaxFormUnbind()
.bind('submit.form-plugin', options, doAjaxSubmit)
.bind('click.form-plugin', options, captureSubmittingElement);
};

上面摘录的代码,是JQuery fom插件中ajaxFom这个方法的源代码。
下载地址为:plugins.jquery.com/form/

ajaxForm注释的含义,大致强调这点意思:
用ajaxform,要么form表单中包含可以submit的元素,要么方法在注册submit事件中调用。也就是说,用ajaxForm必须要有触发submit的方法,否则无法提交form。
相比而言,ajaxSubmit这个方法将直接触发form的submit提交。