address.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. layui.define(["form","jquery"],function(exports){
  2. var form = layui.form,
  3. $ = layui.jquery,
  4. Address = {
  5. provinces : function() {
  6. //加载省数据
  7. var proHtml = '',that = this;
  8. $.get("../../json/address.json", function (data) {
  9. for (var i = 0; i < data.length; i++) {
  10. proHtml += '<option value="' + data[i].code + '">' + data[i].name + '</option>';
  11. }
  12. //初始化省数据
  13. $("select[name=province]").append(proHtml);
  14. form.render();
  15. form.on('select(province)', function (proData) {
  16. $("select[name=area]").html('<option value="">请选择县/区</option>');
  17. var value = proData.value;
  18. if (value > 0) {
  19. that.citys(data[$(this).index() - 1].childs);
  20. } else {
  21. $("select[name=city]").attr("disabled", "disabled");
  22. }
  23. });
  24. })
  25. },
  26. //加载市数据
  27. citys : function(citys) {
  28. var cityHtml = '<option value="">请选择市</option>',that = this;
  29. for (var i = 0; i < citys.length; i++) {
  30. cityHtml += '<option value="' + citys[i].code + '">' + citys[i].name + '</option>';
  31. }
  32. $("select[name=city]").html(cityHtml).removeAttr("disabled");
  33. form.render();
  34. form.on('select(city)', function (cityData) {
  35. var value = cityData.value;
  36. if (value > 0) {
  37. that.areas(citys[$(this).index() - 1].childs);
  38. } else {
  39. $("select[name=area]").attr("disabled", "disabled");
  40. }
  41. });
  42. },
  43. //加载县/区数据
  44. areas : function(areas) {
  45. var areaHtml = '<option value="">请选择县/区</option>';
  46. for (var i = 0; i < areas.length; i++) {
  47. areaHtml += '<option value="' + areas[i].code + '">' + areas[i].name + '</option>';
  48. }
  49. $("select[name=area]").html(areaHtml).removeAttr("disabled");
  50. form.render();
  51. }
  52. };
  53. exports("address",Address);
  54. })