我有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 ---------------&gt;
<?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>
答案 0 :(得分:0)
你必须使用像$ stateValues这样的php变量,并且需要将<select> & <option>
标签附加为字符串。最后回显$ stateValues var作为返回的响应。
答案 1 :(得分:0)
PHP中的$ cities数组需要与AJAX GET请求中发送的值匹配的键。您正在发送一个字符串(即“美国”),并尝试将其用作数字索引数组的键。