Home > Web Front-end > JS Tutorial > JavaScript Ajax Json realizes the linkage effect of upper and lower drop-down boxes example code_javascript skills

JavaScript Ajax Json realizes the linkage effect of upper and lower drop-down boxes example code_javascript skills

WBOY
Release: 2016-05-16 17:12:56
Original
1487 people have browsed it

I recently tried to create a drop-down box linkage function between departments and personnel. The corresponding relationship between departments and personnel is 1:N

Copy code The code is as follows:

Department< ;/div>



People




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

How to write js method on this page:

Copy code The code is as follows:






The data returned here contains a list (see list below). 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:

Copy code

The code is as follows:


public String departmentChangeEvent() throws Exception{
  userList=service.queryForList("Workorder.queryUserByDepartmentCode", departmentCode);
  if(userList!=null&&userList.size()>0)
  {
   HttpServletResponse response = ServletActionContext.getResponse();
   response.setContentType("text/html;charset=utf-8");
   response.setHeader("Pragma","No-cache");
   response.setHeader("Cache-Control","no-cache");
   response.setHeader("Cache-Control", "no-store");
   PrintWriter writer = response.getWriter();
   JSONObject json = new JSONObject();
   Map map = new HashMap();
      map.put("list",userList);
      JSONObject jso = JSONObject.fromObject(map);
   writer.write(jso.toString());
      writer.flush();
      writer.close();  }
         return null; 
   } 

这个方法是部门切换事件,通过departmentCode(field域,有set,get)来求的当前部门下的用户放到userList中。

然后通过标准写法把userList放到一个定义好的HashMap中,KEY为list。

复制代码 代码如下:

JSONObject jso = JSONObject.fromObject(map); 

这是最为关键的一步,有的json对象创建方法也可以为JSONObject jso = new JSONObject() ; 然后把list里的记录放入到jso中。。。

在这里是行不通的,前台会认为返回的是个字符串。。。

struts中 返回类型为json

复制代码 代码如下:

 
      
   
 
       

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