【发布时间】:2015-03-18 16:37:11
【问题描述】:
我正在尝试根据用户在前一个字段中的输入设置 jquery 自动完成输入。
我有一个 php 脚本,它向这个 jquery post 函数返回一个 json 变量。但是之后我似乎无法正确设置我的阵列。
我尝试只为数据设置一个变量并在 $.post 函数之外处理数组,但仍然没有运气。
我只是不确定当“父”值显示为 null 时,我的数组的子值如何以及为什么会正确发出警报?
function populateMetrics(light_id){
var availableMetrics = [];
$.post(
"getLightMetrics.php",
{
light_id: light_id,
},
function(data) {
$.each(data, function(index, item){
alert(index); //correct index
availableMetrics[index] = [];
availableMetrics[index]['value'] = item.benchmark_id;
alert(availableMetrics[index]['value']); //correct value
alert(availableMetrics[index]); //null?
availableMetrics[index]['label'] = item.benchmark_variant + "-" + item.benchmark_metric;
alert(availableMetrics[index]['label']); //correct
alert(item.benchmark_id + " = " + item.benchmark_variant + "-" + item.benchmark_metric);
alert(availableMetrics[index]); //still null
});
alert(availableMetrics); //all null, but correct amount
$( "#metric" ).autocomplete({
source: availableMetrics,
focus: function( event, ui ) {
$( "#metric" ).val( ui.item.label );
return false;
},
select: function( event, ui ) {
$( "#metric" ).val( ui.item.label );
$( "#metric_id" ).val( ui.item.value );
return false;
}
});
},
"json"
);
}
【问题讨论】:
-
仅供参考,不要使用警报来调试代码,请使用您的控制台。但你的意思是:
alert(availableMetrics[index]);显示null?null和空值是有区别的,只是说
标签: javascript jquery json post multidimensional-array