Rob*_*Rob 1 jquery jquery-mobile
处理返回json响应的表单帖子的最佳做法是什么?我们正试图在我们网站的移动版本中重用一些返回JSON的代码,我不确定处理javascript的最佳方法.我想填充一个对话框.我真的必须在表单标记上将data-ajax设置为false并调用$ .post吗?
谢谢,罗布
是的,为了在jQuery Mobile中处理表单提交,您必须添加data-ajax="false"到表单标记,以便jQuery Mobile框架不会为您处理它.然后,您可以为submit事件设置自己的处理程序:
//add event handler to your form's submit event
$('form').on('submit', function (e) {
var $this = $(this);
//prevent the form from submitting normally
e.preventDefault();
//show the default loading message while the $.post request is sent
$.mobile.showPageLoadingMsg();
//send $.post request to server, `$this.serialize()` adds the form data to the request
$.post($this.attr('action'), $this.serialize(), function (response) {
//you can now access the response from the server via the `response` variable
$.mobile.hidePageLoadingMsg();
}, 'json');//you can set the response data-type as well
});
Run Code Online (Sandbox Code Playgroud)
这是以下文档$.post():http://api.jquery.com/jquery.post/
注意:.on()用于代替折旧.bind()功能:http://api.jquery.com/on/
| 归档时间: |
|
| 查看次数: |
7555 次 |
| 最近记录: |