1

私はajaxとJqueryが初めてです。私が設計しているフォームで ajax 呼び出しによってコンボ ボックスを自動入力するコードを作成しました。ここで、コンボ ボックスから選択した値に基づいて、同じフォームの残りのフィールドを自動入力したいと考えています。そのためには、1 つのオブジェクトのみを返す別の 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*/ 
});

実際、ここでは、返された学生オブジェクトにアクセスする方法を決定できないため、上記のようにそのフィールドにアクセスできます。ですから、誰かが親切にこれを行うのを手伝ってくれたら、本当に感謝します。また、より良いアプローチがあれば提案してください。ありがとうございました。

4

1 に答える 1

0

ajax 呼び出しから 1 人の学生の 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);
        }    
    });
 });
于 2013-09-15T08:44:10.007 に答える