在 JQuery Mobile 的两个不同文件中的两个页面之间传递参数 [英] pass parameters between two pages in two different files in JQuery Mobile

查看:25
本文介绍了在 JQuery Mobile 的两个不同文件中的两个页面之间传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Jquery 中的两个页面之间传递参数的最佳方式是什么?.

假设我有一个 page1.html 和 page2.html

我想将参数 id 从 page1 传递到 page2.

这样做的最佳方法是什么?.

我目前使用:

$.mobile.navigate("#workOrderDetailsPage?id=" + woId);

但那是针对单个页面中的两个页面.但是如何使用 JQuery Mobile 为两个单独文件中的两个页面执行此操作?.

解决方案

页面转换之间的数据/参数操作

可以在页面转换期间将参数从一个页面发送到另一个页面.可以通过几种方式完成.

参考:https://stackoverflow.com/a/13932240/1848600

解决方案 1:

您可以使用 changePage 传递值:

$.mobile.changePage('page2.html', { dataUrl : "page2.html?paremeter=123", data : { 'paremeter' : '123' }, reloadPage : true, changeHash : true});

并像这样阅读它们:

$(document).on('pagebeforeshow', "#index", function (event, data) {var 参数 = $(this).data("url").split("?")[1];;parameter = parameters.replace("parameter=","");警报(参数);});

[示例][3]:

index.html

<头><meta charset="utf-8"/><meta name="viewport" content="widdiv=device-widdiv, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/><meta name="apple-mobile-web-app-capable" content="yes"/><meta name="apple-mobile-web-app-status-bar-style" content="black"/><标题><link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css"/><script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"><script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script><脚本>$(document).on('pagebeforeshow', "#index",function () {$(document).on('click', "#changePage",function () {$.mobile.changePage('second.html', { dataUrl : "second.html?paremeter=123", data : { 'paremeter' : '123' }, reloadPage : false, changeHash : true });});});$(document).on('pagebeforeshow', "#second",function () {var 参数 = $(this).data("url").split("?")[1];;parameter = parameters.replace("parameter=","");警报(参数);});<身体><!-- 主页--><div data-role="page" id="index"><div data-role="header"><h3>第一页

<div data-role="内容"><a data-role="button" id="changePage">Test</a>

<!--内容--></div><!--page-->

second.html

<头><meta charset="utf-8"/><meta name="viewport" content="widdiv=device-widdiv, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/><meta name="apple-mobile-web-app-capable" content="yes"/><meta name="apple-mobile-web-app-status-bar-style" content="black"/><标题><link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css"/><script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"><script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script><身体><!-- 主页--><div data-role="page" id="second"><div data-role="header"><h3>第二页

<div data-role="内容">

<!--内容--></div><!--page-->

解决方案 2:

或者您可以为存储目的创建一个持久的 javascript 对象.只要 ajax 用于页面加载(并且页面不会以任何方式重新加载),该对象就会保持活动状态.

var storeObject = {名 : '',姓 : ''}

示例:http://jsfiddle.net/Gajotres/9KKbx/

解决方案 3:

您还可以像这样访问上一页的数据:

$('#index').live('pagebeforeshow', function (e, data) {警报(data.prevPage.attr('id'));});

prevPage 对象保存完整的上一页.

解决方案 4:

作为最后一个解决方案,我们有一个漂亮的 localStorage 的 HTML 实现.它仅适用于 HTML5 浏览器(包括 Android 和 iOS 浏览器),但所有存储的数据都通过页面刷新保持不变.

if(typeof(Storage)!=="undefined") {localStorage.firstname="龙";localStorage.lastname="Gaic";}

示例:http://jsfiddle.net/Gajotres/J9NTr/

有关此内容的更多信息可以在我的博客文章.

what is the best way for me to pass parameters between two pages in Jquery ?.

Let's say I have a page1.html and page2.html

and I want to pass the parameter id from page1 to page2.

What is the best way of doing this ?.

I currently use :

$.mobile.navigate("#workOrderDetailsPage?id=" + woId);

but that's for a two pages within the single page. But how can I do this for two pages in two separate files using JQuery Mobile ?.

解决方案

Data/Parameters manipulation between page transitions

It is possible to send a parameter/s from one page to another during page transition. It can be done in few ways.

Reference: https://stackoverflow.com/a/13932240/1848600

Solution 1:

You can pass values with changePage:

$.mobile.changePage('page2.html', { dataUrl : "page2.html?paremeter=123", data : { 'paremeter' : '123' }, reloadPage : true, changeHash : true });

And read them like this:

$(document).on('pagebeforeshow', "#index", function (event, data) {
    var parameters = $(this).data("url").split("?")[1];;
    parameter = parameters.replace("parameter=","");  
    alert(parameter);
});

[Example][3]:

index.html

<!DOCTYPE html>
  <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="widdiv=device-widdiv, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <title>
    </title>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js">
    </script>
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>  
    <script>
        $(document).on('pagebeforeshow', "#index",function () {
            $(document).on('click', "#changePage",function () {     
                $.mobile.changePage('second.html', { dataUrl : "second.html?paremeter=123", data : { 'paremeter' : '123' }, reloadPage : false, changeHash : true });
            }); 
        }); 

        $(document).on('pagebeforeshow', "#second",function () {
            var parameters = $(this).data("url").split("?")[1];;
            parameter = parameters.replace("parameter=","");  
            alert(parameter);
        });         
    </script>
   </head>
   <body>
    <!-- Home -->
    <div data-role="page" id="index">
        <div data-role="header">
            <h3>
                First Page
            </h3>
        </div>
        <div data-role="content">
          <a data-role="button" id="changePage">Test</a>
        </div> <!--content-->
    </div><!--page-->

  </body>
</html>

second.html

<!DOCTYPE html>
  <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="widdiv=device-widdiv, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <title>
    </title>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js">
    </script>
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>  
   </head>
   <body>
    <!-- Home -->
    <div data-role="page" id="second">
        <div data-role="header">
            <h3>
                Second Page
            </h3>
        </div>
        <div data-role="content">

        </div> <!--content-->
    </div><!--page-->

  </body>
</html>

Solution 2:

Or you can create a persistent javascript object for a storage purpose. As long ajax is used for page loading (and page is not reloaded in any way) that object will stay active.

var storeObject = {
    firstname : '',
    lastname : ''
}

Example: http://jsfiddle.net/Gajotres/9KKbx/

Solution 3:

You can also access data from the previous page like this:

$('#index').live('pagebeforeshow', function (e, data) {
    alert(data.prevPage.attr('id'));
});   

prevPage object holds a complete previous page.

Solution 4:

As a last solution we have a nifty HTML implementation of localStorage. It only works with HTML5 browsers (including Android and iOS browsers) but all stored data is persistent through page refresh.

if(typeof(Storage)!=="undefined") {
    localStorage.firstname="Dragan";
    localStorage.lastname="Gaic";            
}

Example: http://jsfiddle.net/Gajotres/J9NTr/

More about this can be found in my blog article.

这篇关于在 JQuery Mobile 的两个不同文件中的两个页面之间传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆