我是新来的AJAX和jQuery。我已经写了code在我的表格Ajax调用,我设计到自动填充组合框。现在,我想做出同样的形式领域休息autopoulated基于从组合框中选择的值,并为我需要另一个Ajax调用,这将返回我学生。有了这个目标我可以设置像这样的形式的领域:
I am new to ajax and Jquery. I have written the code to autopopulate a combo box by an ajax call in my form that I am designing. Now I want to make rest of the fields of the same form autopoulated based on the selected value from combo box and for that I need another ajax call which will return me only one object of student
. With this object I can set the fields of the form like this way:
$('#student_name)VAL(student.name)。
$('#student_roll_num)VAL(student.roll_num)。等等。
$('#student_name').val(student.name);
$('#student_roll_num').val(student.roll_num); etc.
其中name,roll_num的形式领域的ID。
where name, roll_num are the id of the fields of the form.
我写这个方式如下:
//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*/
});
其实在这里我不能决定如何访问重新调校的学生对象,以便我可以访问它的领域,如上面我说的。所以,如果有人好心帮我做这件事,我将非常感激。此外,如果有任何更好的方法请建议。谢谢你。
Actually here I cannot decide how to access the retuned student object so that I can access its fields like above I said. So, if anyone kindly help me to do this, I will be really grateful. Also if there is any better approach please suggest. Thank you.
假设你从你的Ajax调用得到一个学生的JSON,这应该工作:
Assuming you get json of one student from your ajax call, this should work:
$("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);
}
});
});