2013-10-27 33 views
0

我有2个下拉选择框,其中一个从列表中选择您的国家,下一个下拉框应填充相应的州或地区。出于某种原因,'州/地区'字段将不会显示任何基于从第一个下拉框中选择的选项,即'国家/地区'字段。下拉选择器不从数组中提取信息

我确实在一点工作,试图恢复原来的代码的方式,但我不能让它继续工作了。任何解决方案在那里?

这里是我的HTML代码--------------->

<script type="text/javascript" src="../jquery.js"></script> 

<script type="text/javascript"> 
    jQuery(document).ready(function(){    
     // when any option from country list is selected 
     jQuery("select[name='Country']").change(function(){   

      // get the selected option value of country 
      var optionValue = jQuery("select[name='Country']").val();  

      /** 
      * pass country value through GET method as query string 
      * the 'status' parameter is only a dummy parameter (just to show how multiple parameters can be passed) 
      * if we get response from data.php, then only the cityAjax div is displayed 
      * otherwise the cityAjax div remains hidden 
      * 'beforeSend' is used to display loader image 
      * 'complete' is used to hide the loader image 
      */   
      jQuery.ajax({ 
       type: "GET", 
       url: "country-state-data.php", 
       data: { 
        Country: optionValue, 
        status: 1 
       }, 
       beforeSend: function(){ jQuery("#ajaxLoader").show(); }, 
       complete: function(){ jQuery("#ajaxLoader").hide(); }, 
       success: function(response){ 
        jQuery("#cityAjax").html(response); 
        jQuery("#cityAjax").show(); 
       } 
      });   
     }); 
    }); 
</script> 

<?php 

?> 
<select name="Country" id="country"> 
    <option value="">-Country-</option> 
    <option value="United States">United States</option> 
    <option value="United Kingdom">United Kingdom</option> 
    <option value="Canada">Canada</option> 
    <option value="Australia">Australia</option> 
    <option value="Brazil">Brazil</option> 
    <option value="France">France</option> 
    <option value="Italy">Italy</option> 
    <option value="New Zealand">New Zealand</option> 
    <option value="South Africa">South Africa</option> 
</select> 
<div id="ajaxLoader" style="display:none"><img src="../ajax-loader.gif" alt="loading..."></div> 

<div id="cityAjax" style="display:none"> 
    <select name="State" id="state"> 
     <option value="">-State/Region-</option> 
    </select> 
</div> 
<br> 
<input type="hidden" name="tempt" id="tempt" value="http://" /> 
<input type="hidden" name="tempt2" id="tempt2" class="clear" value="" /> 

<button class="button2" ontouchstart="('touchstart');">SUBSCRIBE</button> 
</form> 

</section> 

这里是PHP -------------- - >

<?php 
$country = $_GET['Country']; 

if(!$country) { 
    return false; 
} 

$cities = array(
      1 => array('Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'), 

      2 => array('Avon', 'Bedfordshire', 'Berkshire', 'Buckinghamshire', 'Cambridgeshire', 'Cheshire', 'Cleveland', 'Cornwall', 'Cumbria', 'Derbyshire', 'Devon', 'Dorset', 'Durham', 'East Sussex', 'Essex', 'Gloucestershire', 'Hampshire', 'Herefordshire', 'Isle of Wight', 'Kent', 'Lancashire', 'Leicestershire', 'Lincolnshire', 'London', 'Merseyside', 'Middlesex', 'Norfolk', 'Northhamptonshire', 'Northumberland', 'North Humberside', 'North Yorkshire', 'Nottinghamshire', 'Oxfordshire', 'Rutland', 'Shropshire', 'Somerset', 'South Humberside', 'South Yorkshire', 'Staffordshire', 'Suffolk', 'Surrey', 'Tyne and Wear', 'Warwickshire', 'West Midlands', 'West Sussex', 'West Yorkshire', 'Wiltshire', 'Worcestershire'), 

      3 => array('Alberta', 'British Columbia', 'Manitoba', 'New Brunswick', 'New Foundland and Labrador', 'Northwest Territories', 'Nova Scotia', 'Nunavut', 'Ontario', 'Prince Edward Island', 'Quebec', 'Saskatchewan', 'Yukon Territory'), 

      4 => array('Australian Capital Territory', 'New South Wales', 'Northern Territory', 'Queensland', 'South Australia', 'Tasmania', 'Victoria', 'Western Australia'), 

      5 => array('Acre', 'Alagoas', 'Amapa', 'Amazonas', 'Bahia', 'Ceara', 'Distrito Federal', 'Espirito Santo', 'Goias', 'Maranhao', 'Mato Grosso', 'Mato Grosso do Sul', 'Minas Gerais', 'Para', 'Paraiba', 'Parana', 'Pernambuco', 'Piaui', 'Rio de Janeiro', 'Rio Grande do Norte', 'Rio Grande do Sul', 'Rondonia', 'Roraima', 'Santa Catarina', 'Sao Paulo', 'Sergipe', 'Tocantins'), 

      6 => array('Alsace', 'Aquitaine', 'Auvergne', 'Brittany', 'Burgundy', 'Centre', 'Champagne-Ardenne', 'Franche-Comte', 'Ile-de-France', 'Languedoc-Roussillon', 'Limousin', 'Lorraine', 'Lower Normandy', 'Midi-Pyrenees', 'Nord-Pas-de-Calais', 'Pays de la Loire', 'Picardy', 'Poitou-Charentes', 'Provence-Alpes-Cote d\'Azur', 'Rhone-Alpes', 'Upper Normandy', 'Corsica', 'French Guiana', 'Guadeloupe', 'Martinique', 'Mayotte', 'Reunion'), 

      7 => array('Abruzzo', 'Aosta Valley', 'Apulia', 'Basilicata', 'Calabria', 'Campania', 'Emilia-Romagna', 'Friuli-Venezia Giulia', 'Lazio', 'Liguria', 'Lombardy', 'Marche', 'Molise', 'Piedmont', 'Sardinia', 'Sicily', 'Trentino-Alto Adige/Sudtirol', 'Tuscany', 'Umbria', 'Veneto'), 

      8 => array('Auckland', 'Bay of Plenty', 'Canterbury', 'Gisborne', 'Hawke\'s Bay', 'Manawatu-Whanganui', 'Marlborough', 'Nelson', 'Northland', 'Otago', 'Southland', 'Taranaki', 'Tasman', 'Waikato', 'Wellington', 'West Coast'), 

      9 => array('Eastern Cape', 'Free State', 'Gauteng', 'KwaZulu-Natal', 'Limpopo', 'Mpumalanga', 'North West', 'Northern Cape', 'Western Cape'), 
     ); 

$currentCities = $cities[$country]; 
?> 

<select name="State" id="state"> 
    <option value="">-State/Region-</option> 
    <?php 
    foreach($currentCities as $state) { 
     ?> 
     <option value="<?php echo $state; ?>"><?php echo $state; ?></option> 
     <?php 
    } 
    ?> 
</select> 
+1

*“我确实有一点工作,试图恢复”* - 不会解决您的问题,但是一个建议;始终处理(工作)代码的副本。这是一个艰难的教训,我们都走过了这条道路,包括我自己,只是没有“代码”,但更难以重现(长篇故事) - “我学会了艰难的道路,有时是最好的办法。” –

+0

哪部分工作?使用浏览器控制台检查请求并查看发送的内容,返回的内容以及是否引发任何错误。当不确定问题出在哪里时,很难排除客户端和服务器代码。这有点像说'我的车赢了,开始,怎么了' – charlietfl

回答

0

PHP中的$ cities数组需要与AJAX GET请求中发送的值匹配的键。您正在发送一个字符串(即“美国”),并试图将其用作数字索引数组的关键字。

+1

非常感谢。有效!大声笑 –

0

你必须要像$ stateValues一个PHP变量,需要将<select> & <option>标签追加为字符串。最后回显$ stateValues var作为对返回的响应。