如果您使用自定义事件处理程序处理表单提交,submit
则可以在同一页面上处理验证:
//bind an event handler to the submit event for your login form
$(document).on('submit', '#form_id', function (e) {
//cache the form element for use in this function
var $this = $(this);
//prevent the default submission of the form
e.preventDefault();
//run an AJAX post request to your server-side script, $this.serialize() is the data from your form being added to the request
$.post($this.attr('action'), $this.serialize(), function (responseData) {
//in here you can analyze the output from your server-side script (responseData) and validate the user's login without leaving the page
});
});
要阻止 jQuery Mobile 运行自己的表单 AJAX 合并,请将其放在表单标签上:
<form data-ajax="false" action="...">