JQuery中的AjaxForm和AjaxSubmit使用差不多功能也差不多。很容易误解。
按照作者的解释:
AjaxForm
ajaxForm不能提交表单。在document的ready函数中,使用ajaxForm来为AJAX提交表单进行准备。提交动作必须由submit开始
ajaxSubmit
马上由AJAX来提交表单。你可以在任何情况下进行该项提交。
option的参数
var options = {
target: '#output1', // target element(s) to be updated with server response
beforeSubmit: showRequest, // pre-submit callback
success: showResponse // post-submit callback
// other available options:
//url: url // override for form's 'action' attribute
//type: type // 'get' or 'post', override for form's 'method' attribute
//dataType: null // 'xml', 'script', or 'json' (expected server response type)
//clearForm: true // clear all form fields after successful submit
//resetForm: true // reset the form after successful submit
// $.ajax options can be used here too, for example:
//timeout: 3000
};
示例代码摘自:http://www.malsup.com/jquery/form/#code-samples
ajaxForm
The following code controls the HTML form beneath it. It uses ajaxForm to bind the form and demonstrates how to use pre- and post-submit callbacks
// prepare the form when the DOM is ready
$(document).ready(function() {
var options = {
target: '#output1', // target element(s) to be updated with server response
beforeSubmit: showRequest, // pre-submit callback
success: showResponse // post-submit callback
// other available options:
//url: url // override for form's 'action' attribute
//type: type // 'get' or 'post', override for form's 'method' attribute
//dataType: null // 'xml', 'script', or 'json' (expected server response type)
//clearForm: true // clear all form fields after successful submit
//resetForm: true // reset the form after successful submit
// $.ajax options can be used here too, for example:
//timeout: 3000
};
// bind form using 'ajaxForm'
$('#myForm1').ajaxForm(options);
});
// pre-submit callback
function showRequest(formData, jqForm, options) {
// formData is an array; here we use $.param to convert it to a string to display it
// but the form plugin does this for you automatically when it submits the data
var queryString = $.param(formData);
// jqForm is a jQuery object encapsulating the form element. To access the
// DOM element for the form do this:
// var formElement = jqForm[0];
alert('About to submit: \n\n' + queryString);
// here we could return false to prevent the form from being submitted;
// returning anything other than false will allow the form submit to continue
return true;
}
// post-submit callback
function showResponse(responseText, statusText) {
// for normal html responses, the first argument to the success callback
// is the XMLHttpRequest object's responseText property
// if the ajaxForm method was passed an Options Object with the dataType
// property set to 'xml' then the first argument to the success callback
// is the XMLHttpRequest object's responseXML property
// if the ajaxForm method was passed an Options Object with the dataType
// property set to 'json' then the first argument to the success callback
// is the json data object returned by the server
alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
'\n\nThe output div should have already been updated with the responseText.');
}
ajaxSubmit
The following code controls the HTML form beneath it. It uses ajaxSubmit to submit the form.
// prepare the form when the DOM is ready
$(document).ready(function() {
var options = {
target: '#output2', // target element(s) to be updated with server response
beforeSubmit: showRequest, // pre-submit callback
success: showResponse // post-submit callback
// other available options:
//url: url // override for form's 'action' attribute
//type: type // 'get' or 'post', override for form's 'method' attribute
//dataType: null // 'xml', 'script', or 'json' (expected server response type)
//clearForm: true // clear all form fields after successful submit
//resetForm: true // reset the form after successful submit
// $.ajax options can be used here too, for example:
//timeout: 3000
};
// bind to the form's submit event
$('#myForm2').submit(function() {
// inside event callbacks 'this' is the DOM element so we first
// wrap it in a jQuery object and then invoke ajaxSubmit
$(this).ajaxSubmit(options);
// !!! Important !!!
// always return false to prevent standard browser submit and page navigation
return false;
});
});
// pre-submit callback
function showRequest(formData, jqForm, options) {
// formData is an array; here we use $.param to convert it to a string to display it
// but the form plugin does this for you automatically when it submits the data
var queryString = $.param(formData);
// jqForm is a jQuery object encapsulating the form element. To access the
// DOM element for the form do this:
// var formElement = jqForm[0];
alert('About to submit: \n\n' + queryString);
// here we could return false to prevent the form from being submitted;
// returning anything other than false will allow the form submit to continue
return true;
}
// post-submit callback
function showResponse(responseText, statusText) {
// for normal html responses, the first argument to the success callback
// is the XMLHttpRequest object's responseText property
// if the ajaxSubmit method was passed an Options Object with the dataType
// property set to 'xml' then the first argument to the success callback
// is the XMLHttpRequest object's responseXML property
// if the ajaxSubmit method was passed an Options Object with the dataType
// property set to 'json' then the first argument to the success callback
// is the json data object returned by the server
alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
'\n\nThe output div should have already been updated with the responseText.');
}
分享到:
相关推荐
3. 对目标表单元素添加`submit`事件监听器,使用`$.ajaxForm`或`$.ajaxSubmit`方法来处理表单提交。 4. 在提交事件的处理函数中,调用`$.confirm`以弹出确认对话框,根据用户的选择决定是否执行表单提交。 通过这样...
然后,通过选择器找到目标表单,调用`.ajaxForm()`或`.ajaxSubmit()`方法来启用Ajax提交功能。例如: ```html <script src="jquery.js"> <script src="jquery.form.js"> $(document).ready(function() { $('#...
3. **初始化表单**:在jQuery的选择器中找到你的表单元素,然后调用`.ajaxForm()`或`.ajaxSubmit()`方法。例如: ```javascript $('#myForm').ajaxForm({ success: function(response) { // 提交成功后的处理...
jQuery Form Plugin能够让你简洁的将...插件里面主要的方法, ajaxForm和ajaxSubmit,能够从form组件里采集信息确定如何处理表单的提交过程。 两个方法都支持众多的可选参数,能够让你对表单里数据的提交做到完全的控制。
在提到的标题和描述中,".ajaxSubmit"是一个jQuery插件的方法,用于处理表单的Ajax提交,这允许用户在不刷新整个页面的情况下发送数据到服务器。而“jquery-migrate-1.2.1.js”和“ajax.js”是两个关键的JavaScript...
只需在表单中添加`enctype="multipart/form-data"`属性,然后使用`ajaxForm`或`ajaxSubmit`,插件会自动处理文件上传。 6. **进度条与状态显示** 对于大文件上传,插件可以显示进度条和当前状态。通过监听`...
接下来,调用`ajaxForm`或`ajaxSubmit`方法初始化插件: ```javascript form.ajaxForm(options); ``` 或者 ```javascript form.ajaxSubmit(options); ``` 其中`options`是可选参数,用于设置异步提交的配置。 #...
1. **异步表单提交**:通过使用`$.ajaxForm()`或`$.ajaxSubmit()`方法,可以实现异步表单提交。例如: ```javascript $("#myForm").ajaxForm({ success: function(response) { // 提交成功后的回调函数,response...
jQuery Form插件的核心在于其`.ajaxForm()`和`.ajaxSubmit()`两个方法。 1. **`.ajaxForm()`**:该方法会绑定一个事件处理器,当表单被提交时,自动以Ajax方式发送请求。例如: ```javascript $('#myForm').ajax...
3. **初始化插件**:使用jQuery选择器选取表单,然后调用`$.ajaxForm`或`$.fn.form`方法进行初始化。 ```javascript $('#myForm').ajaxForm({ beforeSubmit: function(arr, form, options) { // 预提交操作,如...
### JQuery Form 插件中的 `ajaxForm()` 和 `ajaxSubmit()` 方法详解 #### 一、引言 在 Web 开发中,使用 AJAX 进行异步表单提交是一种常见的技术手段,它允许开发者无需重新加载整个页面即可提交数据并接收响应...
jQuery Form插件是jQuery库中的一个强大工具,它极大地简化了HTML表单的处理,提供了异步提交(AJAX)功能,使得用户在无需页面刷新的情况下就能发送表单数据,提高了用户体验。这个插件依赖于jQuery核心库,如在本...
Demo 4 : form插件的使用--ajaxForm()和ajaxSubmit(). Demo 5 : form插件的使用--验证后提交(简单验证). Demo 6 : form插件的使用--验证后提交. Demo 7 : form插件的使用--验证后提交. Demo 8 : form插件的使用-...
除了`ajaxSubmit`,jQuery Form Plugin还提供了`ajaxForm`、`serializeArray`、`serialize`等方法,这些方法同样增强了对表单的处理能力。`ajaxForm`是用于自动绑定表单的提交事件,`serializeArray`和`serialize`...
通过`.ajaxForm()`和`.ajaxSubmit()`方法,表单数据会按照一定的格式被序列化并发送到服务器。序列化表单的方法有两种:`formSerialize()`和`fieldSerialize()`。前者会序列化表单内的所有字段,而后者则可以指定...
$('#ajaxForm').on('submit', function(e) { e.preventDefault(); // 阻止默认提交 $.ajax({ url: $(this).attr('action'), // 提交的URL,通常为控制器方法 type: 'POST', // 提交方式 data: $(this)....
2. 初始化表单:通过`$.ajaxForm`或`$("#formId").ajaxSubmit`来绑定表单事件。 ```javascript $("#myForm").ajaxForm({ success: function(response) { console.log('表单提交成功:', response); }, error: ...
通过以上步骤,我们可以利用jQuery.form.js与Struts 1.x实现页面多个表单的AJAX提交,同时保持页面的其他部分不变,从而提升Web应用的交互性和用户体验。在实际项目中,可能还需要根据具体需求进行更复杂的定制和...