繁体   English   中英

JQuery Mobile-在动态生成的页面之间传递变量

[英]JQuery Mobile - Passing Variables Between Dynamically Generated Pages

我想我应该因为JQuery-Mobile的出色表现而感到高兴,但是我仍然无法想象为什么JQueryMobile没有传递变量的本地方法。

无论如何,我试图传递一些变量以在动态创建的“页面”上创建自定义内容。

页面是动态创建的。 但是,我一直无法传递一个变量,并且已经尝试了几天。

任何帮助都是极好的。

对于传递变量的最新尝试,我正在尝试CameronAskew的$ .mobile.paramsHandler.addPage代码 ,该代码开箱即用。

除了通用的jquery-mobile起点之外, 我还使用“此脚本开始”,它可以很好地动态制作页面,如下所示:

// Prepare your page structure
var newPage = $("<div data-role='page' id='page'><div data-role=header><a data-iconpos='left' data-icon='back' href='#' data-role='button' data-rel='back'>Back</a><h1>Dynamic Page</h1></div><div data-role=content>Stuff here</div></div>");

// Append the new page into pageContainer
newPage.appendTo($.mobile.pageContainer);

// Move to this page by ID '#page'
$.mobile.changePage('#page');

这是完整的javascript:

run();
setInterval(function(){    
    run();
},5000);

function run() {
    var output;
    $.ajax({
        url: "http://[url_here]/mobile_get_data_2.php",
        success: function( data ) {
            var obj = jQuery.parseJSON( data );
            var output='';
            var charts='';
            var idx=1;          
            $.each(obj,function(k,v){
                output += '<ul data-role="listview" data-count-theme="a" data-inset="true">';
                output += '<a data-count-theme="a" href="#chart?param1=212121&param2=32327"><li>[returned description here]';
                output += '<span class="ui-li-count">';
                output += [returned count here];
                output += '</span></a></li>';
                output += '</ul>';
                idx = (idx+1);
            });
            $('#sensors').html(output).trigger("create");
        }
    });
}

$('#sensors').on('click', function ( e) {
    // Prepare your page structure
    var newPage = $(
        "<div data-role='page' id='page'>"
        + "<div data-role=header>"
        + "<a data-iconpos='left' data-icon='back' href='#' data-role='button' data-rel='back'>Back</a>"
        + "<h1>Chart</h1>"
        + "</div>"
        + "<div data-role=content>Chart Here</div>"
        + "</div>"
    );
   newPage.appendTo($.mobile.pageContainer); // Append the new page into pageContainer

    // Move to this page by ID '#page'
     $.mobile.changePage('#page');     
});

// CameronAskew
$(function () {
    $.mobile.paramsHandler.addPage(
        "page",                      // jquery mobile page id which will accept parameters
        ["param1", "param2"],       // required parameters for that page
        [],                          // optional parameters for that page,
        function (urlVars) {
            // $("#param1display").html(urlVars.param1);
            // $("#param2display").html(urlVars.param2);
            alert( urlVars.param1);
        }
    );

    $.mobile.paramsHandler.init();
});

和HTML:

<div data-role="page" id="summary">
<div data-role="header"><h1>MakerSpace</h1></div>
<ul id="sensors" data-role="listview" data-count-theme="c" data-inset="true"></ul></div>
<div data-role="footer"></div>
</div>

星星对齐!

改变了这个

$('#sensors').on('click', function ( e) {

对此

$('ul').on('click', function () {

将变量放入(显然)

output += '<a data-count-theme="a" href="#chart?param1=[returned id]"><li>[returned description]';

现在就可以了。

我希望这可以帮助某人...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM