Home > Article > Web Front-end > JavaScript Ajax Json realizes linkage between upper and lower drop-down boxes
最近尝试做出一个部门和人员的下拉框联动功能,部门和人员的对应关系是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>