-1

我有 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; 

?>
4

2 回答 2

0

改变这个:

<p><label>CITY</label>
    <select id='cityID' name='cityID'>  </select>
</p>

对此:

<p><label>CITY</label>
    <select id='cityID' name='cityID'>
    <?php
    if (isset($userDetails['areaID']))
    {
        $query = mysql_query("SELECT * FROM `cities` WHERE area_id = {$userDetails['areaID']} ORDER BY id ASC "); 
        while($index = mysql_fetch_array($query)) 
        {
            $db_city_id = $index['id'];
            $db_city_name = $index['name'];
            if (isset($userDetails['cityID']) && $db_city_id == $userDetails['cityID'])
                echo "<option value='$db_city_id' selected>$db_city_name</option>";         
            else    
                echo "<option value='$db_city_id'>$db_city_name</option>";
        }
    }
    else
    {
        echo '<option value="0">Select Area...</option>';
    }
    ?>
    </select>
</p>
于 2013-09-08T14:12:17.650 回答
0

尝试这样的事情:

$(".country").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;

$.ajax
({
type: "POST",
url: "ajax_city.php",
data: dataString,
cache: false,
success: function(html)
{
$(".city").html(html);
} 
});

});

.country 和 .city 都是选择下拉菜单,ajax_city.php 获取所有城市并输出 html。

于 2013-09-08T15:26:13.070 回答