在jquery函数里面如何追加一个对象。价值
这里student是一个包含值name,admissionNo.etc ..
的对象 $('<td></td>').val(item['id']).html(item['student.name'])
运行代码时, student.name 未显示,警告显示未定义
function onTestStudentChange() {
$.ajax({
type : 'POST',
dataType : 'json',
url : '/sample/selectTestStudent.html',
data : ({
id : $('#test').val()
}),
success : function(responseData) {
if (responseData != null) {
$('#studentTest').find('td').remove().end().append('').val('-1');
$.each(responseData,function(index, item) {
$('#studentTest').append(
$('<td></td>').val(item['id']).html(item['student.name']),
$('<tr></tr>').val(item['id']).html(item['']) );
alert("Alert"+item['student.name']);
});
} else {
$('#studentTest').find('td').remove().end().append(
'<td th:text="${student}"></td>').val('-1');
}
}
});
}
Json值:
{"id":1,"student":{"id":51,"name":"Nimal","fatherName":"Adhithya","motherName":"Anila","admissionNo":"002","division":{"id":4,"divisionName":"B","standardId":{"id":7,"standardName":"LPKG","schoolId":{"schoolId":3,"schoolCode":"003","schoolName":"CMSHSS","schoolAddress":"TSR","schoolStreet":"Round south","schoolCity":"Thrissur","district":{"districtId":1,"districtName":"Thrissur","state":{"id":1,"stateName":"KERALA","country":{"countryId":10,"countryName":"INDIA","createdDate":"Oct 3, 2013","isDelete":false},"createdDate":"Aug 19, 2013","isDelete":false},"createdDate":"Aug 19, 2013","isDelete":false},"state":{"id":1,"stateName":"KERALA","country":{"countryId":10,"countryName":"INDIA","createdDate":"Oct 3, 2013","isDelete":false},"createdDate":"Aug 19, 2013","isDelete":false},"country":{"countryId":10,"countryName":"INDIA","createdDate":"Oct 3, 2013","isDelete":false},"schoolPhone":"1204536789","schoolEmail":"cms@ymail.com","schoolFax":"9876543210","status":false,"createdDate":"Dec 12, 2013","isDelete":false},"createdDate":"Aug 20, 2013","isDelete":false},"createdDate":"Aug 24, 2013","isDelete":false},"phoneNo":9804758123,"status":false,"createdDate":"Dec 18, 2013 12:00:00 AM","isDelete":false},"test":{"id":1,"testName":"Numbers","createdDate":"May 9, 2013 12:00:00 AM","isDelete":false},"correct":10,"wrong":0,"notAttend":0,"mark":100,"max_mark":100,"attendDate":"Dec 12, 2013 12:00:00 AM","createdDate":"Dec 12, 2013 12:00:00 AM","IsDelete":false}" ViewReport.html:162
答案 0 :(得分:3)
var student = item["student"];
if(student) {
var name = student["name"];
}