javascript Jquery Mobile Listview Clicked Item - 将参数传递到另一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9083347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:33:11  来源:igfitidea点击:

Jquery Mobile Listview Clicked Item - Pass parameter to another page

javascriptjquerylistviewjquery-mobile

提问by Erhan H.

I've a index.html page. Also this page contains lots of page like #home, #list #contacts etc.

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

in #list part i dynamically get data from my webpage and generate listview. I want that, when user click any of list item, redirect to #imageDetail page and pass image URL to page and show image

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

here is the #imageDetail page part

这是#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>

And below code is my javascript code to get json data dynamically.

下面的代码是我动态获取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>

The problem is after page change alert(brewername)fires. But list all imageurls that listed in listview not my selected.

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

How can i fixed this issue

我该如何解决这个问题

Thanks in advance.

提前致谢。

回答by Phill Pafford

jQM Docs:

jQM 文档:

This is just quoting the docs but if you read the page it should give you an idea on how to accomplish this.

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

The application uses links with urls that contain a hash that tells the application what category items to display:

应用程序使用包含哈希值的 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>

回答by aki

Well, this is my way and works very good.

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

HTML

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

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

This works for me like a charm and i'm using it very often!

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

回答by Saurabh

live event has been deprecated, use 'on',

现场活动已被弃用,请使用“开”,

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

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