Ajax achieves three levels of select Association,

Source: Internet
Author: User

Ajax achieves three levels of select Association,

This article uses ajax technology to obtain data from the database and dynamically implement select three-level association, as shown below:

The front-end js Code is as follows:

<Script type = "text/javascript" src = "jquery/jquery-1.9.1.min.js"> </script> <script type = "text/javascript"> $ (document ). ready (function () {$. ajax ({type: "get", dataType: "text", url: "GetItemAction? Itemlevel = 1 ", success: function (data) {// converts a JSON string to a JSON object var objJSON = eval (" ("+ data + ")"); var len = objJSON. itemInfo. length; var objSelect = document. getElementById ("firstlevel"); for (var I = 0; I <len; I ++) {var op = new Option (objJSON. itemInfo [I]. itemname, objJSON. itemInfo [I]. itemcode); objSelect. add (op) ;}, error: function (XMLHttpRequest, textStatus, errorThrown) {alert (errorThrown) ;}); // display level-2 Category func Tion firstlevel_Click () {var objfirst = document. getElementById ("firstlevel"); var index = objfirst. selectedIndex; // select the index var itemcode = objfirst. options [index]. value; // select the value if (itemcode = 0) {alert ("select event category"); return;} // Delete the second-level category and retain the first var objsecond = document. getElementById ("secondlevel"); for (var I = objsecond. options. length-1; I> 0; I --) {objsecond. options. remove (I);} // Delete the third-level category. retain the first var objthird = document. getElementById (" Thirdlevel "); for (var I = objthird. options. length-1; I> 0; I --) {objthird. options. remove (I) ;}$. ajax ({type: "get", dataType: "text", url: "GetItemAction? Itemlevel = 2 & itemcode = "+ itemcode, success: function (data) {// convert a JSON string to a JSON object var objJSON = eval (" ("+ data + ") "); var len = objJSON. itemInfo. length; var objSelect = document. getElementById ("secondlevel"); for (var I = 0; I <len; I ++) {var op = new Option (objJSON. itemInfo [I]. itemname, objJSON. itemInfo [I]. itemcode); objSelect. add (op) ;}, error: function (XMLHttpRequest, textStatus, errorThrown) {alert (errorThrown ); });} // Display the third-level classification function secondlevel_Click () {var objsecond = document. getElementById ("secondlevel"); var index = objsecond. selectedIndex; // select the index var itemcode = objsecond. options [index]. value; // select the value if (itemcode = 0) {alert ("select event category"); return;} // Delete Level 3 category keep the first var objthird = document. getElementById ("thirdlevel"); for (var I = objthird. options. length-1; I> 0; I --) {objthird. options. remove (I) ;}$. ajax ({type: "get", dataType: "text ", Url:" GetItemAction? Itemlevel = 3 & itemcode = "+ itemcode, success: function (data) {// convert a JSON string to a JSON object var objJSON = eval (" ("+ data + ") "); var len = objJSON. itemInfo. length; var objSelect = document. getElementById ("thirdlevel"); for (var I = 0; I <len; I ++) {var op = new Option (objJSON. itemInfo [I]. itemname, objJSON. itemInfo [I]. itemcode); objSelect. add (op) ;}}, error: function (XMLHttpRequest, textStatus, errorThrown) {alert (errorThrown );}});}

Some html code

<Table class = "table-exposure"> <tr> <td height = "40"> <select id = "firstlevel" onchange = "firstlevel_Click ()" style = "width: 99%; font-size: 1em; "> <option value =" 0 "> select an event Category </option> </select> </td> </tr> <td height =" 40"> <select id = "secondlevel" onchange = "secondlevel_Click () "style =" width: 99%; font-size: 1em; "> <option value =" 0 "> select event classes </option> </select> </td> </tr> <td height =" 40"> <select id = "thirdlevel" style = "width: 99%; font-size: 1em; "> <option value =" 0 "> select an event Category </option> </select> </td> </tr> </table>

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.