1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>二级联动菜单实现</title>
    6. <script type="text/javascript" src="js/jquery.js"></script>
    7. <script type="text/javascript">
    8. $(function(){
    9. $.ajax({
    10. "url":"http://192.168.4.127:8080/channel",
    11. "data":{"level":"1"},
    12. "type":"get",
    13. "dataType":"json",
    14. "success":function(json){
    15. console.log(json);
    16. for(var i=0;i<json.length;i++){
    17. var ch = json[i];
    18. $("#lv1").append("<option value='"+ ch.code +"'>"+ ch.name +"</option>");
    19. }
    20. }
    21. })
    22. })
    23. $(function(){
    24. $("#lv1").on("change",function(){
    25. var parent = $(this).val();
    26. console.log(parent);
    27. $.ajax({
    28. "url":"http://192.168.4.127:8080/channel",
    29. "data":{"level":"2","parent":parent},
    30. "dataType":"json",
    31. "type":"get",
    32. "success":function(json){
    33. console.log(json);
    34. //移除lv2下的所有原始options项
    35. $("#lv2>option").remove();
    36. for(var i=0;i<json.length;i++){
    37. var ch = json[i];
    38. $("#lv2").append("<option value='"+ ch.code +"'>"+ ch.name +"</option>");
    39. }
    40. }
    41. })
    42. })
    43. })
    44. </script>
    45. </head>
    46. <body>
    47. <select id="lv1" style="width: 200px;height: 30px;">
    48. <option selected="selected">请选择</option>
    49. </select>
    50. <select id="lv2" style="width: 200px;height: 30px;">
    51. </select>
    52. </body>
    53. </html>