在jquery函数中如何附加对象的值

Inside jquery function how to append an object's value

本文关键字:对象 何附加 jquery 函数      更新时间:2023-09-26

在jquery函数内部如何附加一个对象。

这里学生是一个包含值名称、录取号等的对象。

 $('<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
var student = item["student"];
if(student) {
    var name = student["name"]; 
}