【问题标题】:Jquery Mobile Listview Clicked Item - Pass parameter to another pageJquery Mobile Listview Clicked Item - 将参数传递到另一个页面
【发布时间】:2013-10-15 01:36:20
【问题描述】:

我有一个 index.html 页面。此页面还包含许多页面,例如#home、#list #contacts 等。

在#list 部分,我从我的网页动态获取数据并生成列表视图。我希望,当用户单击任何列表项时,重定向到 #imageDetail 页面并将图像 URL 传递到页面并显示图像

这是#imageDetail页面部分

    <div data-role="page"  id="detailedIMAGE" data-theme="a">
      <div data-role="header" data-theme="b" data-position="fixed">
        <h1>Image Detail</h1>
        </div>
            <div data-role="content">       
            <img id="imageDetayURL" name="imageDetayURL" src="glyphish-icons/158-wrench-2.png"/>
            <input type="text" disabled="disabled" id="brewername" name="brewername" />
            </div>
        </div>  
    </div>

下面的代码是我动态获取json数据的javascript代码。

    <script>
    $('#last5').live("click", function() {
        $.ajax({
            url: "http://mysqlservice.com/getdata.json",
            dataType: 'jsonp',
            success: function(json_results){
                $("#imageListDetay").html('');
                console.log(json_results);
                $('#imageListDetay').append('<ul data-role="listview" id="tweetul" data-theme="c"></ul>');
                listItems = $('#imageListDetay').find('ul');
                $.each(json_results.results, function(key) {
                    html = '<h3>'+json_results.results[key].screen_name+'</h3><span id="detailed_image">'+json_results.results[key].resim_url+'</span><img WIDTH=200 HEIGHT=100 src="http://mywebpage.org/upload/'+json_results.results[key].resim_url+'" /><p class="ui-li-bside"><img WIDTH=8 HEIGHT=13 src="images/07-map-marker.png"/> '+json_results.results[key].adres_data+'</p>';
                    listItems.append('<li><a  name="imageDetayGoster" href="#detailedIMAGE">'+html+'</a></li>');
               });
                $('#imageListDetay ul').listview();
            },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                //error
            }
        });
    })


    $("#detailedIMAGE").live("pagebeforeshow", function (e, data) { 
         var brewername = $('#detailed_image',data.prevPage).text();
         $('#brewername').val(brewername);  
         $('#imageDetayURL').attr('src', 'http://mobil.harmankaya.org/'+brewername);
         alert(brewername);
     });
    </script>

问题在于页面更改alert(brewername) 触发后。但是列出所有图片列表视图中列出的不是我选择的网址。

我该如何解决这个问题

提前致谢。

【问题讨论】:

  • 如果您对此仍有疑问,请随时查看我的plug-in,它允许使用参数进行干净的 URL 导航

标签: javascript jquery listview jquery-mobile


【解决方案1】:

jQM 文档:

这只是引用文档,但如果您阅读该页面,它应该会让您了解如何完成此操作。

应用程序使用带有包含哈希的 url 的链接告诉 应用程序显示什么类别的项目:

<h2>Select a Category Below:</h2>
<ul data-role="listview" data-inset="true">
    <li><a href="#category-items?category=animals">Animals</a></li>
    <li><a href="#category-items?category=colors">Colors</a></li>
    <li><a href="#category-items?category=vehicles">Vehicles</a></li>
</ul>

【讨论】:

  • 如果您是 jQuery 和 jQuery Mobile 的新手,请在外出之前搜索 google 以获取相关文档并使用这些库制作应用程序
  • @aki 任何开发人员总是试图理解这一切,然后寻求帮助。不要这么粗鲁。
【解决方案2】:

嗯,这是我的方式,效果很好。

HTML

<div data-role="page" id="myPage">
<div data-role="content" id="myContent">
    <ul data-role="listview" data-inset="true/false/whatever" id="myList"></ul>
</div>
</div>

Javascript

$("#myPage").live("pageshow",function(event){
       // get your id from LINK and parse it to a variable
    var json_list_callback = getUrlVars()[id];

       // verify the URL id
    if (json_list_callback === '') {json_list_callback === ''} //or what value you want

       // define your path to JSON file generated by the ID declared upper
    var json_URL = 'http://your.path.to.json.file.php.json?id=' + json_list_callback;

       // get the JSON data defined earlier and append to your LIST
    $.getJSON(json_URL,function(data){
        var entries = data;
        //populate our list with our json data
        $.each(entries,function(index,entry){
                // i use dummy data here, you can have whatever you want in youre json
            $("#myList").append(
               '<li>' +
                        // remember that this "page.html?id=' + entry.json_id + '" will be the link where getUrlVars will get the id declared earlier in function
                  '<a href="page.html?id=' + entry.json_id + '">' + entry.json_title + '<\/a>' +
               '<\/li>'
            );
        });
          //must refresh listview for layout to render
        $("#myList").listview("refresh");
    });
});
    //this function gets from URL the id, category, whatever you declare like this: getUrlVars()['id'] or getUrlVars()['category'] after last symbol of "?"
    // you can define your own symbol with this function
function getUrlVars() {
var vars = [],
    hash;
var hashes = window.location.href.slice(window.location.href.lastIndexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
}
return vars;
}

这对我来说就像一个魅力,我经常使用它!

【讨论】:

  • 谢谢,但我现在清楚了。我如何为我的代码实施您的解决方案。我真的很困惑。
  • 上面描述的方法是通过一个变量用JSON条目填充一个列表。您可以根据需要很好地添加代码。我评论了代码以帮助您更多。
【解决方案3】:

live event 已被弃用,使用 'on',

示例:$("#detailedIMAGE").on("pagebeforeshow", function (e, data){ // code });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    相关资源
    最近更新 更多