码迷,mamicode.com
首页 > 其他好文 > 详细

省市区联动

时间:2015-10-19 12:08:50      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

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>‘);
                  }
              });
          });

 

省市区联动

标签:

原文地址:http://www.cnblogs.com/wicub/p/4891189.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!