Home > Web Front-end > JS Tutorial > JavaScript Ajax Json realizes linkage between upper and lower drop-down boxes

JavaScript Ajax Json realizes linkage between upper and lower drop-down boxes

高洛峰
Release: 2016-11-26 16:23:36
Original
1168 people have browsed it

最近尝试做出一个部门和人员的下拉框联动功能,部门和人员的对应关系是1:N

[html] 

部门
 

   

 

       

   

 

 

人员
 

   

 

     

   

The onchange() event of the department drop-down box takes an AJAX method and returns a JSON object (the JSON contains a LIST).

js method is written on this page:

[javascript]

The data returned here contains list (see list below), and the list contains two attributes: the person's code and the person's name. Then empty() the personnel drop-down box first, and add a new drop-down box element through the select control append method.

Backend code:

[java]

public String departmentChangeEvent() throws Exception{

                                                                  use using List.size()> ;0)

                                                                                                                                                                                           HttpServletResponse response = ServletActionContext.getResponse();            response.setHeader("Pragma","No-cache ");

Response.Setheader (" Cache-Control "," No-Cache ");

Response.Setheader (" Cache-Control "," No-Store "); twriter ( ; JSONObject jso = JSONObject.fromObject(map);

writer .write(jso.toString());

          writer.flush();                 writer.close();

                                                                                                                                                    using  departmentCode (field field with set, get) to retrieve the users under the current department and put them into the userList. www.2cto.com

Then put the userList into a defined HashMap through standard writing, and the KEY is list.

[java]

JSONObject jso = JSONObject.fromObject(map);

This is the most critical step. Some json object creation methods can also be JSONObject jso = new JSONObject( ); Then put the records in the list into jso. . .

It won’t work here, the frontend will think that the returned value is a string. . . The return type in

struts is json

[html]

                                                                                                                   ;/result>

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template