在 jQuery 移动示例中。提交表单后显示的页面与表单将 POST-ing/GET-ing 到的页面相同。
是否可以将表单提交到 URL 并转换到 DOM 内的另一个页面?
最佳答案
您可以为表单的 submit
事件添加事件处理程序并手动提交表单,然后手动转换到您想要的任何页面。
$(function () {
$('#form_id').bind('submit', function (e) {
e.preventDefault();
$.post('path/to/file.php', $(this).serialize(), function (response) {
$.mobile.changePage('#page_to_goto', {transition: 'slide'});
});
});
});
一些注意事项:
- 您可能需要通过将
data-ajax="false"
添加到表单标记来关闭表单的 AJAX 处理。 e.preventDefault()
阻止表单正常提交,事件处理程序末尾的return false;
应该做同样的事情。$.post
- http://api.jquery.com/jQuery.post/.serialize()
- http://api.jquery.com/serialize/$.mobile.changePage()
- http://jquerymobile.com/demos/1.0rc1/docs/api/methods.html
关于jQuery Mobile - 如何将表单提交到 URL 并转换到 DOM 内的另一个页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7739183/