JAVA EE Project Common knowledge of Ajax technology to implement the Select drop-down list linkage two uses (let you really understand Ajax)

Source: Internet
Author: User

The use of the Ajax drop-down list linkage.

Definition of Ajax:

AJAX is a technique for creating fast, Dynamic Web pages.

AJAX enables Web pages to be updated asynchronously by exchanging small amounts of data in the background with the server. This means that you can update a part of a webpage without reloading the entire page.

An example of an Ajax effect:

When the area is empty, maintain the human condition:

Select a region after the situation: (choose Shucheng to bring out the maintenance staff Xiao Liu)

First, the original ecological JS implementation

XMLHttpRequest is the basis of AJAX

XMLHttpRequest Object

All modern browsers support XMLHttpRequest objects (IE5 and IE6 use ActiveXObject).

XMLHttpRequest is used to exchange data with the server in the background. This means that you can update a part of a webpage without reloading the entire page.

[JavaScript]View Plaincopy
  1. var xmlHttp;
  2. function Createxmlhttprequest () {
  3. if (window. ActiveXObject) {//check whether the browser supports XMLHttpRequest objects
  4. XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP"); Create is not supported
  5. }
  6. else if (window. XMLHttpRequest) {
  7. XmlHttp = new XMLHttpRequest (); Support for direct new
  8. }
  9. }//Create a XMLHTTP object
  10. function Ajaxrequest (url,data,responseresult) {//ajaxrequest is the function that sends the request to the background
  11. Createxmlhttprequest (); //Call to create a XMLHttpRequest object
  12. Xmlhttp.open ("POST", url,true); Specifies the type of request (POST), URL, and whether the request is processed asynchronously (yes)
  13. Xmlhttp.setrequestheader ("Content-type","application/x-www-form-urlencoded"); HTTP headers for POST data like HTML forms
  14. Xmlhttp.onreadystatechange = Responseresult; //Specifies the function to be executed when responding to the ready state in the onReadyStateChange event
  15. Xmlhttp.send (data); //Send data
  16. }

Area Select code for the foreground page:

[Java]View Plaincopy
  1. </td>
  2. <th> Regional:</th>
  3. <td>
  4. <select style="width:152px" name="Areaid" id= "areaid" class="Select_field"onchange= " Getwhmans (this.value) ">
  5. <option value="" Style="color: #999999" >-Please select-</option>
  6. <c:foreach items="${arealist}" var="area" >
  7. <option value="${area.id}" >${area.areaName}</option>
  8. </c:forEach>
  9. </select><font color="Red" >*</font>
  10. </td>

Front Maintenance Person Select page code:

[Java]View Plaincopy
  1. <th> Maintenance Personnel:</th>
  2. <td>
  3. <select id="Whman" style="width:152px" class="Select_field" name="Whman" >
  4. <option value="" Style="color: #999999" > Please select </option>
  5. </select><font color="Red" >*</font>
  6. </td>

Here are some of the code behind the scenes

[Java]View Plaincopy
  1. List<whperson> customers = Factorybo.getfugbo (). Getwhperson (area); //Based on the region to obtain the maintainer information for the area Select a drop-down box after the area linkage another drop-down box to bring out the maintainer information for the area
  2. if (Customers! = null) {
  3. Jsonarray Jsonarray = new Jsonarray (); New a JSON array
  4. For (Whperson whman:customers) {
  5. Jsonobject obj = new Jsonobject ();
  6. Obj.put ("id", Whman.getid ());
  7. Obj.put ("name", Whman.getname ());
  8. Jsonarray.add (obj); //Cycle new jsonobject and put the maintainer information in and add it to Josnarray.
  9. }
  10. Out.write (Jsonarray.tostring ()); //output is written to the page below the responsetext inside
  11. } Else {
  12. Out.write ("null");
  13. }
  14. Out.flush ();
  15. Out.close ();

[JavaScript]View Plaincopy
  1. function Responsecustomer () {//function executed after the background response is completed
  2. if (xmlhttp.readystate = = 4) {//4 indicates the request is complete and the response is ready
  3. if (Xmlhttp.status = =) {//means OK
  4. var Message=xmlhttp.responsetext; //text returned to the background
  5. if (message=="null") {//If no information is returned
  6. document.getElementById ("id"). options.length=1; Empty the Option drop-down box for id= ' id '
  7. return false;
  8. }
  9. var Info2 = eval (message); //Parse JSON string
  10. document.getElementById ("Whman"). Options.length=1; Set theoption drop-down box for id= ' ID ' to empty first
  11. $.each (Info2, function (i,n) {
  12. document.getElementById ("Whman"). Options.add (new Option (N.gridname,n.gridid)); Add a drop-down box option to the drop-down box, option plus the value returned in the background
  13. });
  14. }
  15. }
  16. }
  17. Function Getgridbyareaid (val) {///self-written functions another option onchange event-triggered function to select this option after you want the option level to be linked
  18. var url="Village.do?method=getgridbyareaid"; URL to request background
  19. var data="id=" +val.value; Parameters passed into the background
  20. Ajaxrequest (Url,data,responsecustomer); //Real Ajax functions to invoke
  21. }


Second, the implementation of Jquery Ajax

[JavaScript]View Plaincopy
  1. function Getwhmans (obj) {
  2. var url="Bbtj.do?method=getwhman"; URL to request background
  3. $ ("#whman"). empty (); //Pre-empty
  4. $ ("#whman"). Append ($ (' <option value= "" >-Please select-</option> ')); Plus--Please select--Options
  5. if ($ (obj). val () = ="")
  6. Return //No value, return
  7. url+="&areaid=" +$ (obj). val (); URL Parameters
  8. url+="&t=" + (new Date ()). ValueOf (); URL parameters
  9. $.ajax ({
  10. Url:url,
  11. Type:' POST ',//post mode
  12. DataType:' text ',//Return text type
  13. Beforesend:function (xmlhttprequest,status) {
  14. },
  15. Success:function (data,status) {
  16. var d=eval (data); //parsing
  17. $ (d). each (function (index,entity) {
  18. $ ("#whman"). Append ($ (' <option value= "' +entity[' id ']+'" > ' +entity['name ']+' </ Option> ')); //Background Data add to drop-down box
  19. });
  20. },
  21. Complete:function (xmlhttprequest,status) {
  22. },
  23. Error:function () {
  24. }
  25. });
  26. }

JAVA EE Project Common knowledge of Ajax technology to implement the Select drop-down list linkage two uses (let you really understand Ajax)

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.