shumark 2015-11-19
原网址:http://www.360doc.com/content/13/1001/17/1542811_318406421.shtml
一
一般在submit中使用ajax进行
例:
$(function(){
$('#myForm').submit(function(){
$.ajax({
url:"/hsb_workreport/formsub.do",
data:$("#myForm").serialize(),
dataType:"json",
error:function(data){
alert(data);
},
success:function(data){
alert(data);
}
});
});
)
二 使用ajaxForm插件
插件地址:http://malsup.github.io/jquery.form.js
两个主要的API:ajaxForm ajaxSubmit
ajaxForm()配置完之后,并不是马上的提交,而是要等submit()事件,它只是一个准备。一般用法:
$(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).submit(function(){return false;}); });
官方例子没有提交 提交中返回false阻止默认提交 使用ajax提交
其中options的属性,重要的解释一下:
target 返回的结果将放到这个target下 url 如果定义了,将覆盖原form的action type get和post两种方式 dataType 返回的数据类型,可选:json、xml、script clearForm true,表示成功提交后清除所有表单字段值 resetForm true,表示成功提交后重置所有字段 iframe 如果设置,表示将使用iframe方式提交表单 beforeSerialize 数据序列化前:function($form,options){} beforeSubmit 提交前:function(arr,$from,options){} success 提交成功后:function(data,statusText){} error 错误:function(data){alert(data.message);}
ajaxSubmit 实例:
$(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; }); });
其中参数配置大同小异。只是ajaxSubmit()可以任何时刻都能提交!
其他的API:
$('#myFormId').clearForm(); $('#myFormId .specialFields').clearFields(); $('#myFormId').resetForm(); var value = $('#myFormId :password').fieldValue(); var queryString = $('#myFormId .specialFields').fieldSerialize();
另外,官方有一个进度条的demo,可以参考一下:http://www.malsup.com/jquery/form/progress.html