我已经花了好几个小时(而且时间已经不多了)试图弄清楚如何使用 AJAX 从先前选择的状态中填充选择的城市
PHP 文件如下所示:
include_once("../models/class-Zone.php");
$state= $_GET["st"];
$cities= Zone::getCities($state);
echo json_encode($cities);
当我使用 ajax 警告结果时:
$.post(
'../ajax/getcities.php?st='+stateid,
function(data) {
alert(data);
}
);
//I GET THIS:
[{"id":"08078","titulo":"BARANOA"},
{"id":"08001","titulo":"BARRANQUILLA"},
{"id":"08137","titulo":"CAMPO DE LA CRUZ"},
{"id":"08141","titulo":"CANDELARIA"},
{"id":"08296","titulo":"GALAPA"},
{"id":"08132","titulo":"JUAN DE ACOSTA"},
{"id":"08421","titulo":"LURUACO"}]
我还没有找到一种方法来迭代并用这些数据填充 SELECT。选择应该是这样的
<select name="city" id="city">
<option value="ID FROM THE JSON">TITULO FROM THE JSON ARRAY</option>
... AND FOR THE REST OF THE RESULTS
</select>
预先感谢您!我很困惑。