【发布时间】:2020-04-28 14:06:10
【问题描述】:
使用来自 Ajax 请求的变量创建输入按钮和 span 标签,显示每个帖子的点赞数(这是用于留言板),但使用 JQuery 的字段连接超出了我的范围。之前只使用 php/html 进行过这项工作,但想使用 Ajax/JQuery 使其更简洁。
尝试使用JQuery实现以下逻辑:
<div class="content">
<div class="post-action">
<input type="button" value="Like" id="like_<?php echo $ID . "_" . $UserID; ?>" class="like" style="<?php if($type == 1){ echo "color: #ffa449;"; } ?>" /> (<span id="likes_<?php echo $ID . "_" . $UserID; ?>"><?php echo $total_likes; ?></span>)
</div>
</div>
JQuery 基本尝试,但不确定语法/逻辑(没有在页面上填充 html,但 Ajax 请求正在工作):
success: function(response) {
$(".content").html("")
for( var key of Object.keys( response ) ) {
$( '.content' ).append( `<div class="post-action">
<input type="button" value="Like" id="like_${response[key].ID}_${response[key].UserID}></div>`);
}
}
Ajax 请求返回的 JSON 数组数据:
$data[] = array ( 'ID' => $row['ID'], 'UserID' => $row['UserID'], 'UserIDLikeChk' => $row['UserIDLikeChk'], 'MessageText' => nl2br(htmlentities($row['MessageText'],ENT_COMPAT|ENT_IGNORE, "UTF-8") ), 'cntLikes' => $row['cntLikes'], 'Type' => $row['Type'] );
【问题讨论】:
标签: jquery html ajax append concat