【问题标题】:EDITED How to get the json value for localStorage in a loop已编辑如何在循环中获取 localStorage 的 json 值
【发布时间】:2013-03-07 09:29:42
【问题描述】:

在 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部分)的编码

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 字符串的输出类似于

?([{"data":{"id":"4","user_name":"studentB","book":"4567","role":"Student"}}]);

【问题讨论】:

    标签: javascript json cordova local-storage


    【解决方案1】:
    $('#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');
    });
    

    【讨论】:

    • 还是不行,请你看看我上面的最新版本
    • 您是否忘记将这一行 data-name="'+student.data.user_name+'">' 添加到 li 中?
    猜你喜欢
    • 1970-01-01
    • 2017-05-07
    • 2013-11-17
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多