Based on country selected , need to populate state and city

前端 未结 2 1116
失恋的感觉
失恋的感觉 2021-01-16 03:17

HTML:


 
                        
    
提交评论

  • 2021-01-16 03:34

    <!DOCTYPE html>
    <html data-ng-app="myApp">
    
    <head>
      <link rel="stylesheet" href="style.css">
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
      <script src="script.js"></script>
    </head>
    
    <body data-ng-controller="testController">
    
    
    
      <label for="country">Country *</label>
      <select id="country" ng-model="countrySrc" ng-options="country for (country, states) in countries" ng-change="GetSelectedCountry()">
        <option value=''>Select</option>
      </select>
      <label for="state">State *</label>
      <select id="state" ng-disabled="!countrySrc" ng-model="stateSrc" ng-options="state for (state,city) in countrySrc" ng-change="GetSelectedState()">
        <option value=''>Select</option>
      </select>
      <label for="city">City *</label>
      <select id="city" ng-disabled="!countrySrc || !stateSrc" ng-model="city" ng-options="city for city in stateSrc">
        <option value=''>Select</option>
    
      </select>
    
      <script>
        angular
          .module('myApp', [])
          .run(function($rootScope) {
            $rootScope.title = 'myTest Page';
          })
          .controller('testController', ['$scope',
            function($scope) {
    
    
              $scope.countries = {
    
                'USA': {
                  'Alabama': ['Montgomery', 'Birmingham'],
                  'California': ['Sacramento', 'Fremont'],
                  'Illinois': ['Springfield', 'Chicago']
                },
                'India': {
                  'Maharashtra': ['Pune', 'Mumbai', 'Nagpur', 'Akola'],
                  'Madhya Pradesh': ['Indore', 'Bhopal', 'Jabalpur'],
                  'Rajasthan': ['Jaipur', 'Ajmer', 'Jodhpur']
                },
                'Australia': {
                  'New South Wales': ['Sydney'],
                  'Victoria': ['Melbourne']
                }
              };
    
              $scope.GetSelectedCountry = function() {
                $scope.strCountry = $scope.countrySrc;
              };
              $scope.GetSelectedState = function() {
                $scope.strState = $scope.stateSrc;
              };
            }
          ])
      </script>
    </body>
    
    </html>

    Hope this solution helps.

    0 讨论(0)
  • 提交回复
    热议问题