javascript - $.each 传递值给函数

标签 javascript jquery arrays

我不知道如何在 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/

相关文章:

javascript - 尝试返回字符串中数字的总和。如果字符串是 "99Hello1"输出是 100,如果是 "9hello9"答案是 18。遇到无知的错误

python - 如何从包含日期时间对象的数组中进行插值?

javascript - 标题菜单高度增加但 HTML 源代码中没有任何反应?

javascript - 在 THREE.js 中查看 Material 的编译着色器代码?

javascript - React-native 以编程方式滚动多行 TextInput

jquery, float 元素消失后平滑过渡滑动

jquery - Div 选择下拉菜单 jquery 切换和选项

c++ - 在基类中引用不同大小的 std::array 而不是 std::array

javascript - 三JS : Simple City Performance Issue

javascript - 链接日期选择器