jquery Ajax two-level linkage

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.