基于来自Jquery和Ajax的combobox中的选定值自动填充表单

我是ajax和Jquery的新手。 我已经编写了代码来通过我正在设计的表单中的ajax调用来自动填充combobox。 现在我想根据combobox中的选定值自动调整相同表单的其余字段,为此我需要另一个ajax调用,它只返回一个student对象。 使用此对象,我可以像这样设置表单的字段:

$('#student_name').val(student.name); $('#student_roll_num').val(student.roll_num); etc.

其中name,roll_num是表单字段的id。

我是用以下方式写的:

 //This following function will get called when one value from the combo box is //selected $("select#student_id").change(function(){ var student_id = $(this).val(); //Here I am getting the selected value from //the combo box $.ajax({ url: "/students_database?student_id="+student_id, //this is the url that will //send me one student object dataType: "json", success: /*YET TO BE FILLED WITH CODE TO AUTO /* POPULATE REST OF THE FIELDS*/ }); 

实际上在这里,我无法决定如何访问重新调整的学生对象,以便我可以访问上面的字段,我说。 所以,如果有人帮我做这件事,我将非常感激。 如果有更好的方法,请建议。 谢谢。

假设你从ajax调用中得到一个学生的json,这应该有效:

  $("select#student_id").change(function(){ var student_id = $(this).val(); //Here I am getting the selected value from //the combo box $.ajax({ url: "/students_database?student_id="+student_id, //this is the url that will //send me one student object dataType: "json", success: function(student) { $('#student_name').val(student.name); $('#student_roll_num').val(student.roll_num); } }); });