php中的javascript按钮上的innerHTML null
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php中的javascript按钮上的innerHTML null,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2274字,纯文字阅读大概需要4分钟。
内容图文
![php中的javascript按钮上的innerHTML null](/upload/InfoBanner/zyjiaocheng/685/623c1c5671b14922ba96acd9699f6acd.jpg)
我有一个按钮可以在PHP功能中调用javascript函数:
<button id="upvoteCommentButt"'.$rowNr.'" class="btn btn-primary btn-sm" onclick="upvoteComment(\''.$commentID.'\', \''.$rowNr.'\')">
这是Chrome控制台显示一个innerHTML空错误的代码行:
echo '<span class="p" id="commentVotesTxt"'.$rowNr.'> '.$commentVotes.' </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 = " " + results + " ";
// 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号时,它会输出以下错误:
Uncaught TypeError: Cannot set property ‘innerHTML’ of null at Object.success (comments.php?topicID=G1bYixk6B3&topicUserID=vP1zbupMBJ&option=latest:579)
我究竟做错了什么?
谢谢!
解决方法:
这看起来不对.您已经在$rowNr前面输入了ID的结尾“.”因此,所有范围都具有相同的ID“ commentVotesTxt”.
echo '<span class="p" id="commentVotesTxt"'.$rowNr.'> '.$commentVotes.' </span>';
它应该像
echo '<span class="p" id="commentVotesTxt'.$rowNr.'"> '.$commentVotes.' </span>';
内容总结
以上是互联网集市为您收集整理的php中的javascript按钮上的innerHTML null全部内容,希望文章能够帮你解决php中的javascript按钮上的innerHTML null所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。