0

我是 Jquery 和 Ajax 的新手。我已经编写了以下代码来在表单的组合框中单击一个选项后自动填充表单,方法是从这篇文章Autopopulate form based on selected value from combo box with Jquery and Ajax 中获取帮助:

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

但是所有这些值student.email, student.roll_num都是空白的,当我发出语句时alert(student),它就是这样打印[object object]的。但是,当我在浏览器中调用相同的 json 调用时,我得到了预期的学生 json 对象,在该对象中我得到了所有学生属性的正确值。我在上面的代码中的任何地方都做错了吗?因此,如果有人帮助我解决此问题,我将不胜感激。谢谢你。

4

1 回答 1

0

试试这个,这对你有帮助。这里 U = 页面的 Url 和 F = 页面的功能

$("select#student_id").change(function(){
        var student_id = $(this).val();   
    $.ajax({
            type: "POST",
            url: U + '/' + F,
            data: "{id: " + student_id + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            cache: false,
            success: function (response) {
                var content=response.d;
            }
        });
    });
于 2013-09-15T11:12:53.403 回答