Good morning! I generate object literals from form values and I store them in an array as follows
var persons = {};// I want all the objects in the array to be inserted here so that I can stringify it and send via Ajax var array = [];// where I store the object literals var courses = {}; course['name'] = $('#course').val(); array.push(courses); var students = {}; students['first'] = $('#first_name').val(); students['last'] = $('#last_name').val(); students['age'] = $('age').val(); array.push(courses); // I tried this after the help I got this morning but it does not create multiple objects. When there are many successive values, the data sent is empty var persons = { courses: { name: $('#course').val(), }, students: { name: $('#first_name').val(), last: $('#last_name').val(), age: $('#age').val(), }, };
Please does anybody know how I can create several students and have them in the same object with the course name?
Alter your code as follows
courses = {
name: $('#course').val(),
students: []
};
to add a student you do:
courses.students.push({
name: $('#first_name').val(),
last: $('#last_name').val(),
age: $('#age').val(),
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.