我想做马来西亚各州和城市的下拉名单。
状态数据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)
<html>
<head>
<script type="text/javascript">
function showCity()
{
//state id from drop down list
var stateId = state.options[state.selectedIndex].value;
//CODE HERE
}
</script>
</head>
<body>
<form action="test3.php" method="post">
State:
<select name="state" id="state" onchange="showCity();">
<option value ="">select one</option>
<?php
for($i = 0; $i < count($stateJsonObject); $i++)
{
echo '<option value = '.$stateJsonObject[$i] -> stateId.'>';
echo $stateJsonObject[$i] -> stateName;
echo '</option>';
}
?>
</select>
<br />
City:
<select name="city" id="city">
<option value ="">select one</option>
</select>
</form>
</body>
</html>
根据以上参考资料,以下是我的问题:
(1)如何在 CODE HERE 部分比较js和php(json)之间的状态id ?
(2)如何根据我在 CODE HERE 选择的州显示城市下拉列表
部分
答案 0 :(得分:0)
我可以看到你有两个选择:
1。)将整个对象加载到一个&#34; big&#34; JSON并在Javascript中准备就绪 - 然后用JS操纵select。
2。)如果第一名表现不佳(biiiiig数据)&#34;懒惰&#34;加载选择 - 再次使用AJAX - 并使用JS进行渲染选择。
所以 - 在可能的情况下使用JS进行所有操作,我认为这是最简单的:)
p.s。:在编写渲染选择的方法时 - 仔细考虑所有可能的场景并为它们设置重置功能 - 它最终会帮助你:)