Passing data between pages with jQuery Mobile?

我与影子孤独终老i 提交于 2019-11-27 12:31:30
codaniel

One thing that I think is cool about JQM is that you don't have to use parameters to pass data between pages. Since you're in the same DOM as the first page, you can access data using plain old variables, i.e.

field1 = $('[name=field1]').val();
field2 = $('[name=field2]').val();

And so long as you're using the ajax feature of JQM you could do the following in the next page:

$('.title').text(field1);

I made a jsfiddle example for you.

Other ways would be to use the localStorage or sessionStorage api or there are also some plugins mentioned in the docs.

  1. page params
  2. JQM router plugin
Lu Ming

Commonly, there 2 method for transfer parameter between jQuery Mobile page.

  1. Modify Ajax address at first page, and parse the ajax to get parameter in next page.
  2. Using HTML5 sessionStorage, a kind of WebStorage, to transfer parameter.

This is the method use ajax address to transfer parameter. How to pass and get Parameters between two Pages in Jquery Mobile?


Using sessionStorage/localStorage to transfer parameter, you can add this code at first page,

<a href="#page_Parameter1" onclick="sessionStorage.ParameterID=123">
    Before go to next page, parameter id is storaged into sessionStorage.
</a>

In next page, you can use this method to take parameter content,

$('#page_Parameter1').live('pageshow', function(event, ui) {
    alert('Parameter ID: ' + sessionStorage.ParameterID);
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!