<?php header('Content-Type: application/json; charset=utf-8'); echo 'province=[];'; foreach ($arrProvinces as $province) { echo 'province.push(['.$province['ProvinceID'].',"'.$province['ProvinceName'].'"]);'; } echo 'city=[];'; foreach ($arrCitys as $city) { echo 'city.push(['.$city['ProvinceID'].','.$city['CityID'].',"'.$city['CityName'].'"]);'; }
<script type="text/javascript"> function initProvince() { var objProv = jQuery('#locationForm').get(0).province; objProv.options.length = 0; for (var i=0; i<province.length; i++) { objProv.options[i] = new Option(province[i][1], province[i][0]); } } function onProvinceChange() { var objProv = jQuery('#locationForm').get(0).province; var objCity = jQuery('#locationForm').get(0).city; provinceId = objProv.value; objCity.options.length = 0; objCity.options[0] = new Option('請選擇', 0); for (var i=1,j=1; i<=city.length; i++) { if (city[i-1][0] == provinceId) { objCity.options[j++] = new Option(city[i-1][2], city[i-1][1]); } } } jQuery(document).ready(function(){ initProvince(); }); </script>
<form id="locationForm"> <select id="province" onchange="onProvinceChange()"></select> <select id="city"></select> </form>
URL:http://www.bianceng.cn/webkf/PHP/201410/45940.htm