我正在尝试为表单上的隐藏字段指定一个值,我会在单击按钮时附加到div。
这是我的HTML
<div style="margin-bottom:5px" class="comment" data-commentID="<?php echo $c->id;?>">
<div style="border-radius:5px 5px 0px 0px;border:2px solid black;border-bottom-width:0px;padding:3px;font-weight:bold;">
<?php echo $c->user->username?>
</div>
<div style="border-radius:0px 0px 5px 5px;background-color:white;padding:5px;border:2px solid black">
<?php echo $c->comment;?>
<button class="reply-button">Reply</button>
</div>
</div>
这是我的jQuery函数,在回复按钮的单击
上调用$(document).ready(function(){
$(".reply-button").on('click',function(){
var form = $("<form id='reply-form' action='/project/index.php/comments/postReply' method='get'></form>");
form.append("<textarea style='resize:none' rows='4' cols='84' name='comment'></textarea>");
form.append('<input type="hidden" name="topic_id" value="<?php echo $model->id ?>" />')
form.append('<input type="hidden" name="comment_id" value="$(this).parent().parent().attr("data-commentID")"/>')
form.append('<input type="submit" value="Post" />');
$(this).parent().append(form);
$(this).unbind();
});});
如何将comment_id的值设置为data-commentID属性?
如果我警告()我当前设置的值,那么我看到评论ID
alert($(this).parent().parent().attr("data-commentID"));
但是将值设置为,在发布im后得到值为$ $(this).parent()。parent()。attr(
在此先感谢:)
答案 0 :(得分:1)
您需要使用字符串连接将js变量的值分配给字符串文字
form.append('<input type="hidden" name="comment_id" value="' + $(this).closest('.comment').attr("data-commentID") + '"/>')