2013-03-17 17 views
0

的力值,這是我的編碼如何獲得的localStorage

function getStudentList() { 
    $.getJSON('http://mydomain.com/getStudents.php?jsoncallback=?', function(data) { 
     $('#studentList li').remove(); 
     $('#load').hide(); 
     //students = data.user_name; 
     $.each(data, function(index, student) { 
      $('#studentList').append('<li><a href="student_detail.html?user_name=' + student.data.user_name + '">' + 
        '<h4>' + student.data.user_name + '</h4>' + 
        '<p>' + student.data.role + '</p>' + 
        '</a></li>'); 
     }); 
     $('#studentList').listview('refresh'); 
    }); 
} 

我已經看到了一些帖子說的PhoneGap可能不支持像student_detail.html URL的一部分?USER_NAME = XXXX,是不是?因此,我想通過使用localStorage來改變方法。 studentList將生成一個學生名單,我想向用戶顯示所選學生的詳細信息。一旦學生被選中,他的用戶名將被存儲在localStorage中,並獲取其他頁面中的值以供進一步處理。下一次,當用戶想要看另一名學生時,該localStorage將首先被刪除,並創建新的。這種方法好嗎?而且,我的問題是如何保存所選學生用戶名的價值?

var id = $("#id").val(); 
window.localStorage["id"] = id; 

我dunnno如何創建在$。每個

+0

HTTP:/ /coenraets.org/blog/2011/10/sample-application-with-jquery-mobile-and-phonegap/通過引用這篇文章,我想知道他爲什麼可以使用URL像student_detail.html?user_name = XXXX? – HUNG 2013-03-17 22:30:25

+0

如果您已正確設置項目,PhoneGap應該不會遇到訪問您描述的URL的問題。您是否已將getStudents.html和student_detail.html所在的域添加到PhoneGap項目域白名單? http://docs.phonegap.com/zh/2.3.0/guide_whitelist_index.md.html 此外,因爲這是一個PhoneGap項目,所以student_detail.html的鏈接必須是絕對URL,例如HTTP://mydomain.com/student_detail.html USER_NAME = ... – thefrontender 2013-03-17 23:05:13

回答

0

特定#ID的PhoneGap把相對URL,就好像它們是本地文件(文件://)包含在項目中。

如果student_detail.html駐留在服務器上,則應使用完全限定的URL來解決該問題。 http://mydomain.com/student_detail.html?user_name=...

你還需要確保http://mydomain.com在PhoneGap的項目列入白名單按http://docs.phonegap.com/en/2.3.0/guide_whitelist_index.md.html

至於每個li分配一個ID,只需將其添加到您的模板:

... .append('<li id="' + student.data.user_name + '"> ...')