在城市下拉列表中保留价值(第4部分)

时间:2014-11-03 08:07:50

标签: javascript php json

续来自Get state name from drop down list after click submit button (part 3)

  • 状态数据json( $ stateJsonObject

    Array ( 
    [0] => stdClass Object ( [stateId] => s1 [stateName] => Kuala Lumpur) 
    [1] => stdClass Object ( [stateId] => s2 [stateName] => Selangor)
    )
    
  • 城市数据json( $ cityJsonObject

    Array ( 
    [0] => stdClass Object 
    ( [cityId] => c1 [cityName] => Kajang [cityStateId] => s2 ) 
    [1] => stdClass Object 
    ( [cityId] => c2 [cityName] => Seputeh [cityStateId] => s1 ) 
    [2] => stdClass Object ( [cityId] => c3 [cityName] => Shah Alam [cityStateId] => 
    s2 ) 
    [3] => stdClass Object ( [cityId] => c4 [cityName] => Klang [cityStateId] => s2  
    ) 
    [4] => stdClass Object ( [cityId] => c5 [cityName] => Kepong [cityStateId] => s1    
    )
    )
    
  • 代码(test3.php)

    <?php
        $cityState = array();
        $cityName = array();
    
        for($j = 0; $j < count($cityJsonObject); $j++)
        {
            $cityState[] = $cityJsonObject[$j] -> cityStateId;
            $cityName[] = $cityJsonObject[$j] -> cityName;
        }
    ?>
    
    <html>
    <head>
    <script type="text/javascript">
        function showCity(state, target_id)
        {
            var stateId = state.options[state.selectedIndex].value; 
    
            var target = document.getElementById(target_id);
            target.length = 0;
            target.options[0] = new Option('select one', '');
            target.selectedIndex = 0;
    
            var cityState = <?php echo json_encode($cityState, JSON_HEX_QUOT) ?>;
            var cityName = <?php echo json_encode($cityName, JSON_HEX_QUOT)?>; 
    
            for(k = 0; k < cityState.length; k++)
            {
                if(stateId == cityState[k])
                {
                    target.options[target.length] =
                    new Option(cityName[k],cityName[k]);         
                }
            }  
        }
    </script>
    </head>
    
    <body>
    <form action="test3.php" method="post">
        State:
        <select name="state" id="state" onchange="showCity(this, 'city')">
            <option value ="">select one</option>
            <?php
                $select_sign = '';
                for($i = 0; $i < count($stateJsonObject); $i++)
                {
                    if($stateJsonObject[$i] -> stateId == $_POST['state']) 
                    {$select_sign = "SELECTED";}else{$select_sign = "";}
                    echo '<option value = '.$stateJsonObject[$i] -> stateId.' 
                    '.$select_sign.'>';
                    echo $stateJsonObject[$i] -> stateName;
                    echo '</option>';
                }
            ?>
        </select>
    
        <br />
    
        City:
        <select name="city" id="city">
            <option value ="">select one</option>
        </select>
    
        <br />
    
        <input type="submit" name="submit" value="Submit"/>
    </form>        
    </body>
    </html>
    
  • 我的问题是我从状态下拉列表中选择雪兰莪,然后从城市下拉列表中选择 Klang 。单击“提交”按钮后,如何在选定的城市下拉列表中保留 Klang 名称?

1 个答案:

答案 0 :(得分:0)

您可以使用以下任一选项,

  • 使用AJAX提交表单。然后页面将不会重新加载 你可以保持价值不变。

  • 使用HTML5本地存储(或cookie)来跟踪城市名称 应该选择。页面加载时,检查城市的值 已保存,如果已保存,请将选择设置为已保存的值。