jquery realizes the three level linkage between provinces and municipalities

  • 2020-05-24 05:13:14
  • OfStack

Provincial and municipal linkage, with use examples and data table data

Some data are accurate to township level 1!!

Git address: http: / / git oschina. net upliu/province city -- district

Demo code:


<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title> Provinces, 3 Levels of linkage </title>
</head>
<body>
<form method="post" action="post.php">
  <div id="area"></div>
  <input type="submit" style="margin-top: 10px;">
</form>
<script src="jquery-2.1.3.min.js"></script>
<script src="area.js"></script>
<script>
  $(function(){
 
    add_select(0);
 
    $('body').on('change', '#area select', function() {
      var $me = $(this);
      var $next = $me.next();
      /**
       *  If the 1 Class is already a subregion of the currently selected region and is not processed 
       */
      if ($me.val() == $next.data('pid')) {
        return;
      }
      $me.nextAll().remove();
      add_select($me.val());
    });
 
    function add_select(pid) {
      var area_names = area['name'+pid];
      if (!area_names) {
        return false;
      }
      var area_codes = area['code'+pid];
      var $select = $('<select>');
      $select.attr('name', 'area[]');
      $select.data('pid', pid);
      if (area_codes[0] != -1) {
        area_names.unshift(' Please select a ');
        area_codes.unshift(-1);
      }
      for (var idx in area_codes) {
        var $option = $('<option>');
        $option.attr('value', area_codes[idx]);
        $option.text(area_names[idx]);
        $select.append($option);
      }
      $('#area').append($select);
    };
  });
</script>
</body>
</html>

The above is the article to share all the content, I hope you can like.


Related articles: