【发布时间】:2013-03-10 12:34:47
【问题描述】:
我正在尝试用来自 AJAX 的数据填充组合框(输入)。这是为了获取选定州的每个城市,该州在另一个选择控件(州控件)上被选中。
我的尝试:
I'm using the "change" event on the state comboBox, when a state is selected i search for it cities and populate the city control.
$("#state").on("change", function () {
getCities($(this), $("#city"));
});
这实际上是可行的,但我真正的问题是:当我将这些数据带到控件时,我必须单击它来重新填充它(之前的状态数据仍然存在),我希望它会在我完成时完成在状态选择输入上完成状态选择。与这个问题相处的另一个问题是,每次状态选择输入改变它的城市时,选择第一个选项(--- SELECT CITY ---)。函数代码如下:
function getCities(stateControl, cityControl) {
if (stateControl.val()) {
var options = '<option value="0" selected="selected">--- CHOOSE CITY --- </option>';
cityControl.html(options);
var dataString = "state="+stateControl.val();
$.ajax({
type: "POST",
url: "cities.php",
data: dataString,
dataType: "json",
success: function (resposta) {
for(var i=0; i < resposta.length; i++){
options += '<option value="' + city[i].cod_cidade + '">' + cities[i].nome + '</option>';
};
controleCidades.html(options);
}
});
}
}
【问题讨论】:
-
请不要考虑不匹配的变量名,因为代码都是葡萄牙语,我翻译了变量,但有些我忘记了。 (如 respostra 或 cod_cidades)。数据正确,人口问题就是问题。
标签: javascript ajax select input combobox