First implementation of jquery Ajax two-level linkage to download a jquery.js I'm not ready here. Baidu Download
Background: Through the Department ID to find all the classes under the Department
I realize the two-level linkage of the idea is: First query all departments displayed on the page as shown:
Second, the Click event in the Drop-down box gets the ID of the department, and then the department's ID is used to find the class.
Paste Code
Foreground JSP:
<select id= "department" name= "DepartmentID" onchange= "Findclass ()" >
<option value= "0" > Please select </ option>
<c:foreach items= "${departmentall}" var= "D" >
<option value= "${d.departmentid}" >${ d.departmentname}</option>
</c:forEach>
</select>
<select id= "classId" Name= " ClassId ">
<option value=" 0 "> Please select </option>
</select>
Ajax:
function Findclass () {
var DepartmentID = $ ("#department"). attr ("value");
$.ajax ({
URL: "${pagecontext.request.contextpath}/student/findclass",
type: "Get",
timeout: "1000",
Data:{departmentid:departmentid},
success:function (data) {
$ ("#classId option"). Remove ();
$ ("#classId"). Append ("<option value= ' 0 ' > Please select </option>");
if (data!= 0) {for
(var i = 0; i < data.length; i++) {
var classId = data[i].classid;
var className = data[i].classname;
$ ("#classId"). Append ("
<option value=" +classid+ ">"
+ className + "</option>");
}
}
,
error:function (xmlresponse) {
alert (xmlresponse.responsetext);}}
);
This is the code for the background controller:
Inquiry Department:
Modelandview modelandview=new Modelandview ();
list<department> Departmentall = Departmentservice.finddepartmentall ();
Modelandview.setviewname ("Student/add");
Modelandview.addobject ("Departmentall", Departmentall);
return modelandview;
Inquiry class:
@RequestMapping (value= "Findclass") public
String Findclass (httpservletrequest reuqest,httpservletresponse Response) throws exception{
Response.setcontenttype ("Text/json; Charset=utf-8 ");
Gets the department id
String departmentid = Reuqest.getparameter ("DepartmentID");
Integer id = integer.parseint (departmentid);
PrintWriter out = null;
try{out
= Response.getwriter ();
} catch (Exception e) {
e.printstacktrace ();
}
Jsonarray array = new Jsonarray ();
Jsonobject member = NULL;
try{
//According to Department ID to find all classes under the Department
list<classes> classeslist = Classesservice.findclassbydepartmentid (ID);
for (Classes classes:classeslist) {member
= new Jsonobject ();
Member.put ("ClassId", Classes.getclassid ());
Member.put ("ClassName", Classes.getclassname ());
Array.put (member);
}
catch (Exception e) {
e.printstacktrace ();
}
Out.print (Array.tostring ());
return null;
}
Effect chart