tags:

views:

50

answers:

1

How can i poppulate the student name, roll and marks upon selecting the student subject based from select onchange event using jquery. Thank you so much.

My views.py

def add_student(request):   
    subject = Subject.objects.all()
    if request.method == "POST":
        obj = Student()
        obj.name = request.POST['name']
        obj.roll = request.POST['roll']
        obj.subject = Subject.objects.get(name = request.POST['subject'])   
        obj.marks = request.POST['marks']
        obj.save()  

    var = RequestContext(request,{'title': ' Add Student','heading':'ADD STUDENT','subject':subject})
    return render_to_response('student.html',var)

Here is my django template to add dynamic value to the student name , roll and marks textfield..

<form action="." method="POST">
<table>
    <tr>
        <td>Subject</td>
    <td>
        <select id="subject" name= "subject">
            <option value="">- - Select - -</option>
            {% for i in subject %}
                <option>{{ i.name }}</option>
            {% endfor %}            
        </select> 
    </td>
    </tr>
    <tr>
        <td>Student Name</td>
    <td>
        <input id="name" type = "text" name ="name" /> 
    </td>
    </tr>
    <tr>
        <td>Student Roll</td>
    <td>
        <input id="roll" type = "text" name ="roll" /> 
    </td>
    </tr>    
    <tr>
        <td>Marks</td>
    <td>
        <input id="marks" type = "text" name ="marks" /> 
    </td>
    </tr>    
    <tr>
    <td>
        <input id="save" type = "Submit" value="Submit" /> 
    </td>       
    </tr>
</form>
</table>
A: 

students = Array() {% for s in subject %} {% with s.student as st %} {# maybe you call it "s.author" or so... #} students[s.pk] = {"name":st.name,"roll":st.roll,"marks":st.marks} {% endwith %} {% endfor %}

        $("#subject").select(function()
        {
            st = students[$(this).val()]
            $("#name").val(st.name)
            // and so on...
        })
    </script>
joozek