【发布时间】:2014-01-03 15:40:53
【问题描述】:
您好,我已经使用 phonegap 完成了一个适用于 android 的应用程序,现在我正在尝试更改 web 应用程序的代码(该应用程序只是一个注释保存),但是我在更改页面时遇到了问题,经过编辑注释应用程序返回索引页面,当您单击注释以查看单个注释时,应用程序再次返回索引页面(这种情况发生一次,如果您再次单击,工作正常)
演示 notesapp
这是js代码的一部分
editNote: function(e){
var noteId = $("#id-note").attr("value");
var newText = $.trim($("#textarea-edit").val());
var storage = window.localStorage.getItem("notes");
storage = JSON.parse(storage);
var newStorage = [];
for( obj in storage ){
if(noteId == storage[obj].id){
newStorage.push({
id: storage[obj].id,
text: newText,
created: storage[obj].created
});
}else{
newStorage.push(storage[obj])
}
}
window.localStorage.setItem("notes", JSON.stringify(newStorage));
notes.showNotes();
$.mobile.changePage($('#index'));
}
$(document).bind("pagebeforechange", function(e, data){
var u = $.mobile.path.parseUrl( data.toPage )
if(u.hash){
var page = u.hash.split("?")[0],
idNote = u.hash.split("=")[1],
storage = window.localStorage.getItem("notes"),
singleText
;
storage = JSON.parse(storage);
// GET CURRENT NOTE TEXT
for( obj in storage ){
if(idNote == storage[obj].id){
singleText = storage[obj].text;
}
}
// SET ID-NOTE TO A INPUT HIDE FOR LATER RETRIEVE
$("#id-note").attr("value", idNote)
if(page == "#single"){
if(idNote){
console.log("show single page")
$("#single-note").html(singleText);
$("#editnote-button").attr("href","#editnote?id=" + idNote);
}
}else if(page == "#editnote"){
console.log("Show edit")
$("#textarea-edit").val(singleText);
}
}
});
【问题讨论】:
-
我猜问题出在
notes.init()pageinit。您添加多个click事件。例如$("#save-note").off("click").on("click", notes.saveNote);。或将pageinit绑定到#index页面。$(document).on("pageinit", "#index", function ()这将只运行一次init()函数。
标签: javascript jquery-mobile mobile web-applications