我不知道如何在 Stackoverflow 中搜索此问题它提供了不同的解决方案并且与我的问题无关。因此,伙计们,我将再次需要你们的帮助。
变量obj的值:
item.title = Title1 and Title2
item.description = Description1 and Description2
HTML 文件:
function test_pop() {
var fb_id = "xxxxxxxxxxxx";
var v_id = "xxxxxxxx";
$.post('http://mysite.com/myfile.php', {fb_id: fb_id, v_id: v_id}, function(data) {
var obj = $.parseJSON(data);
$.each(obj,function(index, item){
achievement = "You Obtained " + item.title + " " + item.description;
achievement_pop(achievement);
});
});
}
achievement_pop 函数:
function achievement_pop(data) {
$("#achievement_popup").html(data).show(); //DIV that should show up
}
所以问题是,当 div 出现时,它只输出:
<div> <!-- Imagine that this content is inside a DIV -->
**You Obtained Title2 Description2**
</div>
但我想要的输出是:
<div> <!-- Imagine that this content is inside a DIV -->
**You Obtained Title1 Description1**
**You Obtained Title2 Description2**
</div>
希望大家帮帮我,谢谢。
最佳答案
html
函数正在替换 div 的全部内容,而不仅仅是添加。
一个解决方案是替换
$("#achievement_popup").html(data).show();
与
$("#achievement_popup").html($("#achievement_popup").html()+data).show();
但是有点重。就个人而言,我会首先构建 html 并仅使用一次 html
函数。
关于javascript - $.each 传递值给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14118136/