我有 2 个选择框
1.areaID
2.cityID
当用户在第一个选择框中选择区域时,第二个将自动更改并仅显示所选区域的城市。
效果很好,除非我从数据库加载数据或用户提交表单。在这些情况下,未选择正确的城市 - 指针始终位于第一个选项(第一个城市)上。
我需要改变什么?
<p><label>AREA</label>
<select name='areaID' id='areaID'>
<?PHP
$query = mysql_query("SELECT * FROM `areas` ORDER BY id ASC ");
while($index = mysql_fetch_array($query))
{
$db_area_id = $index['id'];
$db_area_name = $index['name'];
if ($db_area_id == $userDetails['areaID'])
echo "<option value='$db_area_id' selected>$db_area_name</option>";
else
echo "<option value='$db_area_id'>$db_area_name</option>";
}
?>
</select><span>*</span>
</p>
<p><label>CITY</label>
<select id='cityID' name='cityID'> </select>
</p>
<script>
<?PHP if ($_POST) { ?>
$(document).ready(function(){
$('#areaID').filter(function(){
var areaID=$('#areaID').val();
var cityID=<?PHP echo $userDetails['cityID'] ?>;
$('#cityID').load('ajax/getCities.php?areaID=' + areaID+'&cityID=' + cityID);
return false;
});
});
<?PHP }else { ?>
$(function () {
function updateCitySelectBox() {
var areaID = $('#areaID').val();
$('#cityID').load('ajax/getCities.php?areaID=' + areaID);
return false;
}
updateCitySelectBox();
$('#areaID').change(updateCitySelectBox);
});
<?PHP } ?>
</script>
获取城市.php:
<?PHP
$areaID = (int) $_GET['areaID'];
$second_option = "";
$query2 = mysql_query("SELECT * FROM `cities` WHERE area_id = $areaID ORDER BY id ASC");
while($index = mysql_fetch_array($query2))
{
$id = $index['id'];
$name = $index['name'];
$name = iconv('windows-1255', 'UTF-8', $name);
$second_option .= "<option value='$id'>$name</option>";
}
echo $second_option;
// exit;
?>