Value Passing In Jquery from one html class to other html class in PhoneGap

后端 未结 2 1940
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 16:25

I am a Iphone developer now moving to phonegap for making cross plateform application.

I have stared learning jqueryMobile for uiDesgin.It is good.

Issue is her

相关标签:
2条回答
  • 2021-02-06 16:48

    I am only guessing the things i've understood from your text but I think what you are searching is how to pass parameters in jQuery Mobile between pages on different HTML (my proposed way 2 is the easiest for different html, otherwise same html different pages take solution 1 or 3).

    There are three ways:

    1) Use HTML5 attributes

    a) in HTML page 1, you've got a link, where you need to know on page two the ID "1234", i hope you understood that this ID can be created dynamically:

    <a data-emp="1234" id="button" href="#home>
    

    b) in HTML page 2 or somewhere else:

    <div id="showParameter"></div>
    

    c) in JS whereever you want e.g. pagebeforeload or onClick(): example is onClick:

    $("#button").on('click', function (data) { 
    var anId = $(this).attr("data-emp"); 
    $("#showParameter").html(anId) 
    });
    

    2) Use the URL / Hashtag to pass a parameter

    via URL:

    a) a link again on page 1:

    <a href="page2.htm?yourID='1234'"></a>
    

    b) JS on page 2 for example on pageInit Event:

    var parameters = $(this).data("url").split("?")[1]; 
    parameter = parameters.replace("yourID=","");
    

    3) via Hashtag: http://api.jquerymobile.com/jQuery.mobile.changePage/ by passing a hashtag, google a little bit there are a lot of ressources.

    now you are able to work with the parameter on page 2 e.g. calling an ajax with parameter or whatever you want.

    hope this helps.

    0 讨论(0)
  • 2021-02-06 16:49

    Solution 1:

    You can pass values with changePage function:

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

    or with a button like this:

    <a href="second.html?paremeter=123" data-role="button">Or through a basic link</a>
    

    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);
    });
    

    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("paremeter=","");  
                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">Pass parameters with changePage function</a>
                            <a href="second.html?paremeter=123" data-role="button">Or through a basic link</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>
    

    This solution is used for multi-HTML template where one jQuery Mobile page is also one HTML file.

    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 info

    If you want to find more information about this topic take a look at this article. You will find everything explained in more details plus working examples.

    0 讨论(0)
提交回复
热议问题