Jquery mobile jquerymobile中的AJAX表单提交
我试图在jQuery移动站点上通过ajax提交一个简单的登录表单,但遇到了麻烦 当我提交表单(通过POST)时,表单参数似乎被添加到url中。不仅如此,他们还删除了我提交表单之前的锚定页面 例如,我在页面Jquery mobile jquerymobile中的AJAX表单提交,jquery-mobile,jquery-validate,ajaxform,Jquery Mobile,Jquery Validate,Ajaxform,我试图在jQuery移动站点上通过ajax提交一个简单的登录表单,但遇到了麻烦 当我提交表单(通过POST)时,表单参数似乎被添加到url中。不仅如此,他们还删除了我提交表单之前的锚定页面 例如,我在页面localhost:8080/myapp/#注册 然后我提交表单,使url变成:localhost:8080/myapp/?email=a@a.com&通过=通过 因此,如果我点击验证错误并单击“返回”按钮,我就不会返回到#注册页面 有什么想法吗?如果您使用自定义的submit事件处理程序处理表
localhost:8080/myapp/#注册
然后我提交表单,使url变成:localhost:8080/myapp/?email=a@a.com&通过=通过
因此,如果我点击验证错误并单击“返回”按钮,我就不会返回到#注册
页面
有什么想法吗?如果您使用自定义的
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 Sumbision,请将其放在表单标签上:
<form data-ajax="false" action="...">
如果要提交表单而不使用ajax(默认设置),则必须在表单字符串中添加“data ajax=“false”:
<form data-ajax="false" action="test.php" method="POST">
上面的Jaspers解决方案适合我!我唯一需要调整的是将.live替换为.submit(.live现在已弃用)。现在是这样的:
$('#form_id').submit(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
});
});
任何关于反对票的反馈都将不胜感激。我只是想知道我做错了什么:)我也会;-)唯一的可能是“live”现在已不推荐使用,您应该改用“on”。甚至可以按顺序“开/关”以防止重复事件:这是上面答案的一部分,介意我问一下为什么要重新发布它吗?请注意,如果表单作为外部页面拉入DOM,则必须在发生后运行。这就是为什么我建议使用委托事件处理程序,这样即使表单在初始页面加载后添加到DOM中,事件处理程序也会捕获事件。