以下代码中的“地图”和“for”如何正确?首先它获取所有唯一ID,然后我尝试将变量从几个“每个”组合并在稍后的ajax中使用它。
Jquery:
$(document).ready(function() {
$('a#export').on('click',function(){
var contentMap = {};
$('[id^="textHolder"],[id^="imgHolder"]').each(function(){
contentMap[this.id] = $(this).html();
});
$('[id^="youtubeHolder"]').each(function(){
var YoutubeSrc = $('iframe',this).attr('src');
var YoutubeHolderID = $(this).attr('id');
});
for(id in contentMap) {
$.ajax({
url: "post.php",
type: "post",
data: {
ExportDivID: id,
ExportDivContent: contentMap[id],
ExportYoutubeSrc: YoutubeSrc,
ExportYoutubeHolderID: YoutubeHolderID
},
success: function(){
alert("success");
},
error: function(){
alert("failure");
}
});
}
});
});
感谢任何帮助!
答案 0 :(得分:0)
for循环解释:
Loop through an array in JavaScript
地图解释:
How to create a hash or dictionary object in JavaScript
变化
for(id in contentMap) { .... }
到
for (var i=0; i<contentMap.length; i++){
if (contentMap[i] == this.id){
$.ajax({
url: "post.php",
type: "post",
data: {
ExportDivID: this.id,
ExportDivContent: contentMap[this.id],
ExportYoutubeSrc: YoutubeSrc,
ExportYoutubeHolderID: YoutubeHolderID
},
success: function(){
alert("success");
},
error: function(){
alert("failure");
}
});
}
}