【问题标题】:keep value in city drop down list(part 4)在城市下拉列表中保留价值(第 4 部分)
【发布时间】:2014-11-03 08:07:50
【问题描述】:

续。来自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>
    
  • 我的问题是我从州下拉列表中选择雪兰莪,然后我从城市下拉列表中选择巴生。点击提交按钮后,我应该如何在城市下拉列表中保持巴生名称被选中?

【问题讨论】:

    标签: javascript php json


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多