您好,我有这个聪明的代码,可以生成按州 ISO 代码排序的确定国家/地区的州列表
countries = new Array();
{foreach from=$countries item='country'}
{if isset($country.states)}
countries[{$country.id_country|intval}] = new Array();
{foreach from=$country.states item='state' name='states'}
countries[{$country.id_country|intval}]['{$state.id_state|intval}'] = '{$state.name|escape:'htmlall':'UTF-8'}';
{/foreach}
{/if}
{/foreach}
结果就是这样,对我来说没问题,因为是按 ISO CODE 订购的
countries = new Array();
countries[10] = new Array();
countries[10]['53'] = 'AG';
countries[10]['54'] = 'AL';
countries[10]['55'] = 'AN';
countries[10]['56'] = 'AO';
countries[10]['58'] = 'AP';
countries[10]['93'] = 'AQ';
countries[10]['57'] = 'AR';
countries[10]['59'] = 'AT';
countries[10]['60'] = 'AV';
countries[10]['61'] = 'BA';
countries[10]['64'] = 'BG';
countries[10]['65'] = 'BI';
countries[10]['62'] = 'BL';
countries[10]['63'] = 'BN';
countries[10]['66'] = 'BO';
countries[10]['69'] = 'BR';
countries[10]['68'] = 'BS';
countries[10]['162'] = 'BT';
countries[10]['67'] = 'BZ';
countries[10]['70'] = 'CA';
countries[10]['72'] = 'CB';
countries[10]['73'] = 'CE';
countries[10]['76'] = 'CH';
countries[10]['156'] = 'CI';
countries[10]['71'] = 'CL';
countries[10]['81'] = 'CN';
countries[10]['77'] = 'CO';
countries[10]['79'] = 'CR';
countries[10]['78'] = 'CS';
countries[10]['74'] = 'CT';
countries[10]['75'] = 'CZ';
countries[10]['82'] = 'EN';
在同一页面中,我有这个 javascript 填充选择 html 标记,但按 ID 而非 ISO CODE 订购选项标签,我不知道为什么
$(document).ready(function(){
$('select#id_country').change(function(){
updateState();
});
updateState();
});
function updateState()
{
$('select#id_state option:not(:first-child)').remove();
var states = countries[$('select#id_country').val()];
if( typeof(states) != 'undefined' )
{
for (indexState in states)
{
//ie bug fix
if (indexState != 'indexOf')
$('select#id_state').append('<option value="'+indexState+'"'+ (idSelectedCountry == indexState ? ' selected="selected' : '') + '">'+states[indexState]+'</option>');
}
$('p.id_state:hidden').slideDown('slow');
}
else
$('p.id_state').slideUp('fast');
}