0

我正在尝试为每条评论添加一个回复按钮,因此当有人单击该按钮(例如“回复”)时,会自动出现一个文本框,用户可以在其中填写表格。

到目前为止我没有做太多...

PHP

<div id='replycomment' class='reply' onclick='jsReply($comment_id);'>Reply</div>
<div id='showreply'></div>

jQuery

function jsReply(comment_id) {
    var form = '<form id="showReplyForm" action"add_reply.php" method="post">';
    form += '<textarea name="replybody" cols="35" rows="2" maxlength="299"</textarea>';
    form += '<input type="hidden" name="replybodyId" value="' + comment_id + '" />'
    form += '<input type="submit" name="replysubmit" value="Reply"/></form>';
    jQuery('#replybody').replaceWith(form); }

任何人都可以帮助我吗?将不胜感激。我对需要将 id 从 jquery 链接到 php 的方式有点困惑......

谢谢

4

3 回答 3

2

To make your code work, do this: http://jsfiddle.net/MaqLQ/1/

within head:

var jsReply = function(comment_id) {
    var form = '<form id="showReplyForm" action"add_reply.php" method="post">';
    form += '<textarea name="replybody" cols="35" rows="2" maxlength="299"></textarea>';
    form += '<input type="hidden" name="replybodyId" value="' + comment_id + '" />';
    form += '<input type="submit" name="replysubmit" value="Reply"/></form>';
    jQuery('#showreply').replaceWith(form);
}

you were missing a semi-colin at the end of one of your form concatenations. you were looking at 'replyBody' rather than the 'showreply' that exists in your html, and if your code wasn't global your html it wouldn't have been defined for the onclick. you were also missing the end angle bracket for your opening textarea tag.

also, i don't know your context and i'm not here for a code review, but among a few other things... you should really avoid using onclick within your html elements.

于 2013-04-19T23:05:26.570 回答
1

PHP

<div class='reply'>Reply</div>
<form action='add_reply.php' method='POST' class='reply-form'>
  <textarea name='replybody'></textarea>
  <input type='hidden' name='comment_id' value='<?php echo $comment_id; ?>' />
  <input type='submit' value='Reply' />
</form>

CSS

.reply-form {
  display: none;
}

Javascript

$(".reply").click(function() {
  $(this).siblings(".reply-form").show();
  $(this).hide();
});
于 2013-04-19T23:05:22.913 回答
1

我推荐使用 jQuery 库。

#showreply通过 CSS隐藏display: none;并将 HTML 表单放入其中。

$('#replycomment').click(function() {
    $('#showreply').show();
});

我还建议将您的 ID 更改为类,因为您将拥有多个实例。

于 2013-04-19T22:58:35.420 回答