省市区联动

html部分

 <tr>
            <th class="textalign">所在城市</th>
            <td>
                <select name="province_id" class="select">
                    <?php if ($order->province_id): ?>
                    <option value="<?=$order->province_id?>"><?=$order->strProvinceName()?></option>
                    <?php else: ?>
                    <option value="">请选择</option>
                    <?php endif; ?>
                      <?php if(isset($province) && count($province)):?>
                      <?php foreach ($province as $key=>$val):?>
                      <option value="<?=$key?>"><?=$val?></option>
                      <?php endforeach;?>
                      <?php endif;?>
                </select>
                <select name="city_id" class="select">
                    <?php if($order->city_id): ?>
                    <option value="<?=$order->city_id?>"><?=$order->strCityName()?></option>
                    <?php else: ?>
                    <option value="">请选择</option>
                    <?php endif; ?>
                </select>
                <select name="country_id" class="select">
                    <?php if($order->country_id): ?>
                    <option value="<?=$order->country_id?>"><?=$order->strCountryName()?></option>
                    <?php else: ?>
                    <option value="">请选择</option>
                    <?php endif; ?>
                  </select>
            </td>
        </tr>

 

js部分代码

   $('select[name="province_id"]').change(function(){
              $.get('<?=url('city/city')?>', {'province_id':$(this).val()}, function(data){
                  if(data.length===0){
                      alert('未查找到下属城市');
                      return;
                  }
                  $('select[name="city_id"]').empty();
                  $('select[name="city_id"]').append("<option value='0'>请选择省</option>");
                  for (var i in data){
                      $('select[name="city_id"]').append('<option value="'+data[i].id+'">'+data[i].name+'</option>');
                  }
              });
          });
          $('select[name="city_id"]').change(function(){
              $.get('<?=url('city/country')?>', {'city_id':$(this).val()}, function(data){
                  if(data.length===0){
                      alert('未查找到下属城市');
                      return;
                  }
                   $('select[name="country_id"]').empty();
                  $('select[name="country_id"]').append("<option value='0'>请选择省</option>");
                  for (var i in data){
                      $('select[name="country_id"]').append('<option value="'+data[i].id+'">'+data[i].name+'</option>');
                  }
              });
          });

 

posted @ 2015-10-19 10:39  午时的海  阅读(222)  评论(0编辑  收藏  举报