在网页中需要使用进行异步提交操作,普通的表单提交并不能实现异步请求。如果需要在一个页面中进行表单的提交,尤其是文件上传操作可以jquery-form.js来进行。
好的,下面进行代码的实现:
html:
<form id="newForm" action="..." method="post">
<input type="input" name="name"/>
<input type="input" name="age"/>
<input type="submit" value="提交"/>
</form>
js:
//最简实现
$("#newForm").ajaxForm(function(result){ //......result为返回值
console.log(result);
});
//Form的参数定义:
var options={
url: url, //form提交数据的地址
type: type, //form提交的方式(method:post/get)
target: target, //服务器返回的相应数据显示在元素(id)好确定
beforeSubmit: function(), //提交前执行的回掉函数
success: function(), //提交成功后执行的回掉函数
dataType:null, //服务器返回的数据类型
clearForm: true, //提交成功后是否清空表单中的字段值
restForm: true, //提交成功后是否充值表单中的字段值,即恢复到页面加载是的状态
timeout: 6000 //设置请求时间,超过时间后,自动退出请求,单位(毫秒)
}
//表单提交--使用参数来实现表单的提交
$("#newForm").ajaxForm(options);