jQuery 在一个对象中使用每个
jQuery use each in an object
我想在我的脚本中使用一个 each
函数,但是这个函数在一个对象中使用,这意味着它 returns 一个错误...我想使用的对象 each
中是 newPlots
。
也许你知道一个窍门?
我想使用 AJAX 返回的内容在 newPlots
中添加绘图。在纯文本中,newPlots 看起来像这样:
var newPlots = {
"City" : {
x : 45.834444,
y : 1.261667,
text : {content : "City"},
tooltip : {content : "City"}
}
, "City2" : {
size:60,
x : 47.323056,
y : 5.041944,
text : {
content : "City",
position : "left",
margin : 5
}
}
}
我想做的是在 newPlots
.
中使用 each
函数
在这里你可以找到我的代码:
$.ajax({
type: "post",
url: '<?php echo $this->Url->build(array('controller'=>'Villes','action'=>'viewresult')); ?>',
data: {
departements: dptcode
},
dataType : "json",
success: function (ret) {
// Update some plots and areas attributes ...
var updatedOptions = {'plots' : {}};
// add some new plots ..
var newPlots = {
$.each(ret, function(index, element) {
"Limoge" : {
x : element.x,
y : element.y,
text : {content : "Limoge"},
tooltip : {content : "Limoge"}
},
});
}
// and delete some others ...
var deletedPlots = [];
$(".mapcontainer").trigger('update', [updatedOptions, newPlots, deletedPlots, {animDuration : 1000}]);
},
error : function(xhr, textStatus, errorThrown) {
alert('An error occurred! ' + errorThrown);
}
});
您尝试扩展 newPlots 的方式在语法上不正确。
尝试
var newPlots = newPlots || { };
$.each(ret, function(index, element) {
newPlots[element.name] = {
x : element.x,
y : element.y,
text : {content : element.name},
tooltip : {content : element.name}
};
});
我想在我的脚本中使用一个 each
函数,但是这个函数在一个对象中使用,这意味着它 returns 一个错误...我想使用的对象 each
中是 newPlots
。
也许你知道一个窍门?
我想使用 AJAX 返回的内容在 newPlots
中添加绘图。在纯文本中,newPlots 看起来像这样:
var newPlots = {
"City" : {
x : 45.834444,
y : 1.261667,
text : {content : "City"},
tooltip : {content : "City"}
}
, "City2" : {
size:60,
x : 47.323056,
y : 5.041944,
text : {
content : "City",
position : "left",
margin : 5
}
}
}
我想做的是在 newPlots
.
each
函数
在这里你可以找到我的代码:
$.ajax({
type: "post",
url: '<?php echo $this->Url->build(array('controller'=>'Villes','action'=>'viewresult')); ?>',
data: {
departements: dptcode
},
dataType : "json",
success: function (ret) {
// Update some plots and areas attributes ...
var updatedOptions = {'plots' : {}};
// add some new plots ..
var newPlots = {
$.each(ret, function(index, element) {
"Limoge" : {
x : element.x,
y : element.y,
text : {content : "Limoge"},
tooltip : {content : "Limoge"}
},
});
}
// and delete some others ...
var deletedPlots = [];
$(".mapcontainer").trigger('update', [updatedOptions, newPlots, deletedPlots, {animDuration : 1000}]);
},
error : function(xhr, textStatus, errorThrown) {
alert('An error occurred! ' + errorThrown);
}
});
您尝试扩展 newPlots 的方式在语法上不正确。
尝试
var newPlots = newPlots || { };
$.each(ret, function(index, element) {
newPlots[element.name] = {
x : element.x,
y : element.y,
text : {content : element.name},
tooltip : {content : element.name}
};
});