【问题标题】:innerHTML null on a javascript button in phpphp中javascript按钮上的innerHTML null
【发布时间】:2018-04-05 05:31:20
【问题描述】:

我有一个在 PHP 函数中调用 javascript 函数的按钮:

<button id="upvoteCommentButt"'.$rowNr.'" class="btn btn-primary btn-sm" onclick="upvoteComment(\''.$commentID.'\', \''.$rowNr.'\')">

这是 Chrome 控制台打印 innerHTML null 错误的代码行:

echo '<span class="p" id="commentVotesTxt"'.$rowNr.'>&nbsp;&nbsp; '.$commentVotes.' &nbsp;&nbsp;</span>';

注意:$rownNr 已被 php 代码成功检索,控制台会打印出来,所以我确信以下 JS 函数可以正确获取:

function upvoteComment(commentID, rowNr) {
    var rowNr = rowNr;

    $.ajax({
        url:"vote-comment.php?isUpvoted=true&commentID=" + commentID, 
        type: "GET", 

            success:function(data) {
                var results = data;  
                console.debug(results);

                document.getElementById("debug").innerHTML = 'VOTES: ' + results + ' -- rowNr: ' + rowNr;

                // Show updated votes
                console.debug("ROW NR: " + rowNr);

                document.getElementById("commentVotesTxt" + rowNr).innerHTML = "&nbsp;&nbsp;" + results + "&nbsp;&nbsp;";

                // Change buttons style
                $( "#upvoteCommentButt" + rowNr ).removeClass( "btn-default" ).addClass( "btn-primary" );
                $( "#downvoteCommentButt" + rowNr ).removeClass( "btn-primary" ).addClass( "btn-default" );

            }, error: function (xhr) {
                document.getElementById("debug").innerHTML = xhr.status;

                if(xhr.status == 418){
                    document.getElementById("errorMessage").innerHTML = "<strong>You already upvoted this comment</strong>";
                    document.getElementById("errorAlert").style.display = 'block';

                } else if (xhr.status == 417){
                    document.getElementById("errorMessage").innerHTML = "<strong>You cannot vote your own comments!</strong>";
                    document.getElementById("errorAlert").style.display = 'block';
                }

                // hide errorAlert
                $("#errorAlert").fadeTo(2000, 500).slideUp(500, function(){
                $("#errorAlert").slideUp(500);
                    document.getElementById("errorMessage").innerHTML = "";
                });
            }

        });

}

当我点击upvoteCommentButt 时,该函数调用一个PHP 脚本并成功执行查询,但是当涉及到更改commentVotesTxt 标记的innerHTML 编号时,它会打印出此错误:

未捕获的类型错误:无法在 Object.success 处设置属性“innerHTML”为 null (cmets.php?topicID=G1bYixk6B3&topicUserID=vP1zbupMBJ&option=latest:579)

我做错了什么?

谢谢!

【问题讨论】:

    标签: javascript php jquery html


    【解决方案1】:

    这看起来不对。您已将 ID 的结尾 " 放在 $rowNr 之前。所以所有span 都有相同的id "commentVotesTxt"。

    echo '<span class="p" id="commentVotesTxt"'.$rowNr.'>&nbsp;&nbsp; '.$commentVotes.' &nbsp;&nbsp;</span>';
    

    应该是这样的

    echo '<span class="p" id="commentVotesTxt'.$rowNr.'">&nbsp;&nbsp; '.$commentVotes.' &nbsp;&nbsp;</span>';
    

    【讨论】:

    • 您在不到一分钟的时间内挽救了我的一天,谢谢!我知道这是一个愚蠢的错误,比如缺少 " 或 ',毕竟总是这样 :)
    • 我会在 8 分钟内接受你的回答(我现在不能这样做,stackoverflow 就是这么说的:)
    • @fvimagination 发生在我们当中。编码愉快。
    • 是的,有趣的是我花了 2 天时间试图找出问题所在,现在我终于可以去洗澡了:LOL:
    猜你喜欢
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 2015-06-01
    • 2013-07-29
    • 2013-01-24
    相关资源
    最近更新 更多