【问题标题】:How to get id from one page to other page?如何从一个页面获取 id 到另一个页面?
【发布时间】:2014-06-02 06:06:57
【问题描述】:

我正在使用 jquery mobiles 1.4.2

我必须将 id 从一页引用到另一页。

html

     <div data-role="page">
     <div data-role="header">
            hello</div>
     <div data-role="content">

     <a href="#freeadd" id="ofrecer"  data-role="button" data-mini="true">Ofrezco en Venta</a>
       <a href="#freeadd" id="ofrecer" class="aliq" data-role="button" data-mini="true">Ofrezco en Alquiler</a>
        <a id="busco" href="#freeadd" data-role="button" data-mini="true">Buscar</a>

         </div>

          </div>


          <div data-role="page" id="freeadd">
         <div data-role="header">
          display id
           </div>
                 </div>

在下一页我想根据 id 显示标题(而不是显示 id)。请任何人帮助我..

【问题讨论】:

  • 您的问题到底是什么?您想使用 php 将数据从一个页面传输到另一个页面吗?
  • 请澄清您的具体问题或添加其他详细信息以准确突出您的需要。正如目前所写的那样,很难准确地说出你在问什么。请参阅“如何提问”页面以获得澄清此问题的帮助。
  • 它的多页编程。我已经使用了两页(data-role="page" 两次),并且我在第一页中给出了第二页的 id。
  • @user3350169 所以你说你的问题中遗漏了很多重要信息?

标签: php jquery string jquery-mobile


【解决方案1】:

存在几种解决方案:

解决方案 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 parameters = $(this).data("url").split("?")[1];;
    parameter = parameters.replace("parameter=","");  
    alert(parameter);
});

示例:

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>

解决方案 2:

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

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

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

解决方案 3:

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

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

prevPage 对象保存完整的前一页。

解决方案 4:

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

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

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

了解更多关于他们的信息here

【讨论】:

  • 它在我的页面中显示警告框,但没有显示“Dragan Gaic”我的意思是它没有显示值
  • 你认为我是通灵者吗? :) 我看不到您的代码,因此无法帮助您。您应该接受这个答案,因为我为您的问题提供了几种解决方案。然后打开另一个并在那里显示您的代码,我会为您修复它。
【解决方案2】:

到另一个页面的链接在哪里?我只看到特定页面部分的链接。

但基本上你只需将该 id 作为 url 参数传递:www.example.com?id=45

比在php中:

if (!empty($_GET['id']) { // will check if argument exists and check if it's not === 0
   $title = $db->getTitleById((int) $_GET['id']);
} 

if (empty($title)){ // Will check if there was id param passed and entry was found in DB
   $title = 'Some default title';
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    相关资源
    最近更新 更多