web前端|js教程
Jquery异步提交
web前端-js教程/docs/jquery.form.plugin/jquery.form.plugin.html#
1. 在你的页面里写一个表单。一个普通的表单,不需要任何特殊的标记:
内容 付费 源码,ubuntu导入色彩配置,tomcat732位下载,拼多多爬虫挂单,php最开始使用的标记是,承德seo营销lzw
小程序源码 百度网盘,vscode关闭程序运行,ubuntu wol 命令,tomcat0丫头,谷歌内核爬虫,php 问号 冒号,宁夏seo推广外包公司,网站程序安装上传修改 asp,网页制作模板源代码免费lzw
分销商城小程序源码,ubuntu登录界面错位,tomcat的图标放在哪里,京东店铺信息爬虫,盘古建站整站php源码,seo系统外包lzw
Name:
Password:
在没有Jquery.Form组件的时候,提交表单,页面会进入阻塞模式,等待服务器端的响应。
2. 引入jQuery和Form Plugin Javascript脚本文件并且添加几句简单的代码让页面在DOM加载完成后初始化表单:
// wait for the DOM to be loaded
$(document).ready(function() {
// bind ‘myForm’ and provide a simple callback function
// 为myform绑定ajaxForm异步提交事件,并提供一个简单的回调函数。
$(‘#myForm’).ajaxForm(function() {
alert(“Thank you for your comment!”);
});
});
加上jquery.form组件后,提交表单时,页面不会再同步提交,而是由js做异步提交,因此提交后页面不会有刷新。
3. 加入能够与服务器端进行交互的回调函数。
$(document).ready(function () {
//options是一个ajaxForm的配置对象。?
var options = {
//target: #output1, // target element(s) to be updated with server response
//beforeSubmit: showRequest, // pre-submit callback
success: callBackFunc // 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
$(#myForm).ajaxForm(options);
});
// responseText是服务端的响应值。statusText是页面
// 提交状态值,success表示成功。
function callBackFunc(responseText, statusText) {
if (statusText == success) {
alert(responseText);
}
else{
alert(“服务端错误!”);
}
}
如果返回的是json数据则回调函数可以这么写
function resultFunction(responseText,statusText) {
if (statusText == success) {
if (responseText.code == 1) {
alert(responseText.message);
}
else {
alert(error occurs!);
}
}
else {
alert(服务器错误!);
}
}
服务端的代码如下:
[HttpPost]
public ActionResult AjaxForm(FormCollection form)
{
string message = "Name:" + form["username"] + " PWD: "+form["password"] ;
//return Content(message);
return Json(new { code = 1, message = message });
}
4. 加入提交前的数据校验函数
为options对象添加 beforeSubmit属性
var options = {
//target: #output1, // target element(s) to be updated with server response
beforeSubmit: checkData, // pre-submit callback
success: callBackFunc // 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
};
// pre-submit callback
function checkData(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;
if ($(formElement).find("#username").val() == "") {
alert("please enter username!");
return false;
} else {
return true;
}
}
验证用户名是否为空,是则提示输入,并取消表单提交。