简体   繁体   中英

Pass json to dropdown

How will I store the List in this servlet in the options of dropdown? What will be the equivalent jquery if the values of Age will be stored as value of option of dropdown and the name will be the display in dropdown

public class testServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
                List<Student> studlist = new ArrayList<Student>();
            Student s1 = new Student();
            s1.setName("Student 1");
            s1.setAge(10);
            Student s2 = new Student();
            s2.setName("Student 2");
            s2.setAge(14);
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(studlist);
        out.print(json);
}
}

TRY:

$(document).ready(function() {
    $('#student').ready(function(event) {  
       $.get('studentServlet',function(responseJson) {   
        var $select = $('#student');                                                 
           $.each(responseJson, function(studlist) {               
               $('<option>').val(this.['age']).text(this['name']).appendTo($select);      
                });
        });
    });
}); 
$(document).ready(function() {
    $('#student').ready(function(event) {  
       $.get('studentServlet',function(responseJson) {   
        var $select = $('#student');                                                 
           $.each(responseJson, function(studlist) {               
               $('<option>').val(this.['age']).text(this['name']).appendTo($select);      
                });
        });
    });
}); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM