我的功能无法正常工作

时间:2013-10-02 16:32:40

标签: javascript jquery ajax function

我正在尝试在页面上的多个项目上显示/隐藏喜欢/不喜欢按钮。所以我正在使用一些JS / jQuery和一些AJAX来完成这项工作,遗憾的是,我做错了但我不知道在哪里。

我通过隐藏的输入获取每个项目的ID:

<input type="hidden" class="id" value="<?php echo $items['id']; ?>">

由于我有几个项目,我正在使用数组:

var idtest = [];
        $(".id").each(function(){
                    idtest.push($(this).val());
            });

然后我想对每个id做一些事情来显示或隐藏喜欢/不喜欢的按钮,但这不起作用。

    for (var i = 0; i<idtest.length; i++){
        var id = idtest[i];
        checkinglikebutton(id);
        nbrlikes(id);

    }

以下是我的功能:

function checkinglikebutton(id){

    $.post("ajax/checkinglikebutton.php", { id: id },
            function(check){
            if (check == 1){
                //show the Like button
                $("#dislike").hide();
            }
            else if (check == 0){
                //show the Dislike button
                $("#like").hide();
            }
            else{
                alert('An error has occured');
            }
    });

}

function nbrlikes(id){

    var action = "nbrlikes";

    $.post("ajax/fetchlikes.php", { id: id, action: action },
            function(nbrlikes){

            $("#nbrlikes").html(nbrlikes + ' Likes');
    });

}

当我只有一个id时,这些功能起作用,因此我所支持的AJAX代码是正确的。

你能告诉我是否有什么问题以及如何解决它。

谢谢!

0 个答案:

没有答案
相关问题