[英]Transfer data from one page to another jQuery Mobile
I using PhoneGap to create a Geolocation App following this excellent tutorial ( link ). 我根据这个出色的教程( 链接 )使用PhoneGap创建了Geolocation App。 Unfortunatelly, I'm having an issue that I can't figure out. 不幸的是,我遇到了一个无法解决的问题。 The relevant parts that are giving me a headache are these: 这些让我头疼的相关部分是:
//Section 1
$('#history').on('pageshow', function () {
tracks_recorded = window.localStorage.length;
$("#tracks_recorded").html("<strong>" + tracks_recorded + "</strong> workout(s) recorded");
$("#history_tracklist").empty();
for (i = 0; i < tracks_recorded; i++) {
$("#history_tracklist").append("<li><a href='#track_info' data-ajax='false'>" + window.localStorage.key(i) + "</a></li>");
}
$("#history_tracklist").listview('refresh');
});
//Section 2
$("#history_tracklist li a").on('click', function () {
$("#track_info").attr("track_id", $(this).text());
});
//Section 3 //第3节
$('#track_info').on('pageshow', function () {
var key = $(this).attr("track_id");
$("#track_info div[data-role=header] h1").text(key);
var data = window.localStorage.getItem(key);
data = JSON.parse(data);
});
Section 1 works just fine, the data is stored, and the list is created without any issues. 第1节工作正常,数据已存储,并且列表已创建,没有任何问题。 But then in Section 2 is when everything goes to hell. 但是在第二部分中,一切都变得井井有条。 By clicking on the element, a new attribute (track_id) is supposed to be created, but it doesn't. 通过单击该元素,应该创建一个新属性(track_id),但不是。 Therefore, in Section 3, the "var key" won't get a value, and as a consequence, "var data" will be null also. 因此,在第3节中,“ var key”将不会获得值,因此,“ var data”也将为null。 As you can imagine, nothing works from there. 可以想象,那里没有任何作用。 What am I doing wrong here? 我在这里做错了什么? I only included what I considered the relevant code, but if more is needed I'll do so. 我只包括了我认为相关的代码,但是如果需要更多的代码,我会这样做。 Thansk! Thansk!
In section 2, I think you just need to delegate click handling to the "#history_tracklist" container, as follows : 在第2节中,我认为您只需要将点击处理委派给“ #history_tracklist”容器,如下所示:
$("#history_tracklist").on('click', "li a", function () {
$("#track_info").attr("track_id", $(this).text());
});
Without delegation you have a rule saying : 没有授权,您有一条规则要说:
when any existing
li a
element within#history_tracklist
is clicked execute my function 当单击#history_tracklist
任何现有li a
元素时,执行我的功能
With delegation, you have a rule saying : 使用授权,您有一条规则说:
when any existing or future
li a
element within#history_tracklist
is clicked execute my function 当单击#history_tracklist
任何现有或将来的li a
元素时,执行我的功能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.