南充免费推广网站,网站增加栏目后面要怎么做,搞一个网站要多少钱,厦门手机网站建设公司如今ajax满天飞#xff0c;作为重点的form自然也受到照顾。 其实#xff0c;我们在平常使用Jquery异步提交表单#xff0c;一般是在submit()中#xff0c;使用$.ajax进行。比如#xff1a; $(function(){$(#myForm).submit(function(){$.ajax({url:/WebTest/test/te…如今ajax满天飞作为重点的form自然也受到照顾。 其实我们在平常使用Jquery异步提交表单一般是在submit()中使用$.ajax进行。比如 $(function(){$(#myForm).submit(function(){$.ajax({url:/WebTest/test/testJson.do,data:$(#myForm).serialize(),dataType:json,error:function(data){alert(data);},success:function(data){alert(data);}});}); }) 这样的方式掩盖了form的功能使它成为了变相的ajax。下面来看看符合form思想的ajaxForm。 ajaxForm: 先下载http://files.cnblogs.com/china-li/jquery.form.js 两个主要的APIajaxForm() 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 forms action attribute //type: type // get or post, override for forms 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 forms action attribute //type: type // get or post, override for forms 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 forms 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 转载于:https://www.cnblogs.com/china-li/archive/2012/12/12/2800144.html