EDITED如何在循环中获取localStorage的json值

时间:2013-03-18 21:15:55

标签: javascript json cordova local-storage

在Oyeme的建议之后

,我已经改变了我的代码,但仍然无法工作。它可以提醒"测试"但无法提醒DBuser_name。

通过以下编码,我可以创建如下图所示的内容。我的问题是,如果用户单击列表中的某个人,我想将student.data.user_name解压缩到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="tutor_student_detail.html?user_name=' + student.data.user_name + '">' +
        $('#studentList').append('<li><a href="tutor_student_detail.html">' +
                '<h4>' + student.data.user_name + '</h4>' +
                '<p>' + student.data.role + '</p>' +
                '</a></li>');

        $("li a").click(function() {
       window.localStorage["view"] = $(this).data('user_name');
    });
    });
    $('#studentList').listview('refresh');
});

}

以下是tutor_student_detail.html(js part)

的编码
function start(){

var localUsername=window.localStorage.getItem("view");

$.getJSON('http://mydomain.com/getStudent.php?user_name='+localUsername+'&jsoncallback=?', displayStudent);

    alert("testing");
}

function displayStudent(data) {
    var DBuser_name=data[0].data.user_name;
    alert(DBuser_name);
    var employee = data.item;
    $('#username').text(student.data.user_name);
    $('#pw').text(student.data.password);
    $('#id').text(student.data.id);


    $('#actionList').listview('refresh');

}

json字符串的输出类似于

  

([{&#34;数据&#34;:{&#34; ID&#34;:&#34; 4&#34;&#34; USER_NAME&#34;:&#34; studentB&# 34;,&#34;书&#34;:&#34; 4567&#34;&#34;作用&#34;:&#34;学生&#34;}}]);

enter image description here

1 个答案:

答案 0 :(得分:2)

$('#studentList').append('<li><a href="tutor_student_detail.html"
                 data-name="'+student.data.user_name+'">' +
                '<h4>' + student.data.user_name + '</h4>' +
                '<p>' + student.data.role + '</p>' +
                '</a></li>');

$("li a").click(function() {
   window.localStorage["view"] = $(this).data('name');
});