经过 Oyeme 的建议,我更改了代码,但仍然无法工作。它可以提醒“testing”,但不能提醒 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"}}]);
最佳答案
$('#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');
});
关于javascript - 编辑如何在循环中获取 localStorage 的 json 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15487093/