省市区联动
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>'); } }); });
作者:Wicub
免责声明:文章、笔记等仅供分享、探讨、参考等学习之用,因此造成的任何后果概不负责。(如有错误、疏忽等问题,欢迎指正、讨论,谢谢)
本文版权归作者和博客园共有,欢迎转载,但请务必在文章页面明显位置给出原文连接,谢谢配合。