2013-10-27 35 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作爲對返回的響應。