Jquery implements the secondary linkage of the drop down menu by using json objects to display the linkage from the DB value

  • 2020-03-30 02:30:10
  • OfStack

Struts2 and Ajax are used to transfer json objects, and then the secondary linkage of menus is realized

The following is the source code of my js file:
 
var mail={ 
//Initialize the
init:{ 
//Initialization data
initdata:{ 
did:'', 
ttitle:'', 
sendpassword:'', 
description:'' 
}, 
//Initialization event
initevent:{ 
DataEvent:function(){ 
$("#did").unbind("change");//Gets a level one menu to bind events
$("#did").bind("change",function(){ 
//alert($(this).val()); 
mail.init.initdata.did=$(this).val(); 
//alert(mail.init.initdata.did); 
mail.init.initevent.getuser(); 
}); 
}, 
<span style="color:#ff0000;">getuser:function(){ 
$.post("mailAction_showUsers?did="+mail.init.initdata.did,null,function(data){ 
var uidoption=$("#uid") ;//Get secondary menu
uidoption.empty();//Empty the list
for(var i=0;i<data.users.length;i++){ 
//Fill it with options, through the loop
uidoption.append("<option value="+data.users[i].uid+ " >"+data.users[i].username+"</option>" ); 
} 
});</span> 
}, 
submitCheck:function(){ 
$("#send").unbind("click"); 
$("#send").bind("click",function(){ 
mail.init.initdata.description=$("input[type='textarea']").text(); 
mail.init.initdata.sendpassword=$("input[name='sendpassword']").val(); 
mail.init.initdata.ttitle=$("input[name='ttitle']").val(); 

if(mail.init.initdata.sendpassword==""){ 
alert(" Please enter your password !"); 
return false; 
} 
else if(mail.init.initdata.ttitle==""){ 
alert(" Please enter the theme! "); 
return false ; 
} 
else if(mail.init.initdata.description==""){ 
alert(" Please enter the content! "); 
return false; 
} 
else 
return true; 
}); 
} 
} 
} 
} 

$().ready(function(){ 
mail.init.initevent.DataEvent(); 
mail.init.initevent.submitCheck(); 

}); 

This is the action in the background:
 
private int did; 
public String getDid(){ 
... 
} 
public void setDid(Strign did){ 
... 
} 
public String showUsers(){ 

users=(ArrayList<User>)this.userService.getUsersByDid(did); 
System.out.println(users.size()+"..."); 
return SUCCESS; 
} 

Struts.xml configuration:
 
<package name="users" namespace="/" extends="json-default"> 
<action name="mailAction_showUsers" method="showUsers" class="mailAction"> 
<result type="json"></result> 
</action> 

</package> 

Foreground JSP page:
 
<td width="65px"> recipient </td> 
<td> <s:select list="departmentlist" listKey="did" listValue="dname" id="did" name="did"></s:select> 
<s:select list="users" listKey="uid" listValue="username" id="uid" name="uid"></s:select> 
</td> 

This is a screenshot of the run after implementation:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201403/201403271121271.gif? 2014227112141 ">  
Write to someone in need... Refer to

Related articles: