用ajax实现省/市/县三联下拉框联动查询功能,可使用json完成
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>三级联动</title> </head> <body> <label for="pro">省</label> <select name="pro" id="pro"></select> <label for="city">市</label> <select name="city" id="city"></select> <label for="area">区</label> <select name="area" id="area"></select> <script src="../../lib/jquery.js"></script> <script> $(function () { $.getJSON('inc/1.json',function (data) { let option = '<option value="">选择(省)</option>'; $.each(data,function (i) { option += '<option value="'+data[i].proId+'">'+data[i].proName+'</option>'; }); $('#pro').html(option); }); $('#pro').change(function () { $.getJSON('inc/2.json',function (data) { let option = '<option value="">选择(市)</option>'; $.each(data,function (i) { if (data[i].proId == $('#pro').val()) { option += '<option value="'+data[i].cityId+'">'+data[i].cityName+'</option>'; } }); $('#city').html(option); }); }); $('#city').change(function () { $.getJSON('inc/3.json',function (data) { let option = '<option value="">选择(区)</option>'; $.each(data,function (i) { if (data[i].cityId == $('#city').val()) { option += '<option value="'+data[i].areaId+'">'+data[i].areaName+'</option>'; } }); $('#area').html(option); }); }); }) </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
总结,思路想到了,但是不会用代码表现出来。