[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
<FONT color=#ff0000>beforeSubmit: checkData, // pre-submit callback
</FONT> success: callBackFunc // 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
};
// 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;
}
}
验证用户名是否为空,是则提示输入,并取消表单提交。
您可能感兴趣的文章: