如何从一个页面到另一个页面获取ID?

时间:2014-06-02 06:06:57

标签: php jquery string jquery-mobile

我正在使用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)显示标题。请任何人帮助我..

2 个答案:

答案 0 :(得分:4)

存在多种解决方案:

解决方案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:

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

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

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

详细了解 here

答案 1 :(得分:0)

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

但基本上你只是把那个id传递给url param: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';
}