3

我有一个 div,我在其中放置了带有提交按钮的 textarea。我需要将 textarea 放在每个帖子下,让用户发表评论。

这是我的文本区域的形状

<div id="textarea_wrap">
   <div id="textarea_12" class="txtarea">
   <form name="post_comment" action="reply.php" method="post"> 
   <input type="hidden" name="post_title" value="Post Title" />
   <input type="hidden" name="post_id" value="12455" />
   <textarea class="textarea" id="txt_12"></textarea>
   <input type="submit" class="button" value="Submit Comment />
   </form>
   </div>
</div>

当我得到 div textarea_wrap 的 innerHtml 并使用 Jquery 将其移动到其他 div 时,它停止工作。

例如

  var txtarea = $("#textarea_wrap").html();
  $("#comment_10").html(txtarea);

它将textarea_wrapdiv的内部html#comment_10 div成功移动,当我使用jquery移动div后查看源代码时,它看起来像这样

<div id="comment_10">
   <div id="textarea_12" class="txtarea">
   <form name="post_comment" action="reply.php" method="post"> 
   <input type="hidden" name="post_title" value="Post Title" />
   <input type="hidden" name="post_id" value="12455" />
   <textarea class="textarea" id="txt_12"></textarea>
   <input type="submit" class="button" value="Submit Comment />
   </form>
   </div>
</div>

但它不起作用,当我点击“提交评论”按钮时,什么也没有发生。如果我在不通过 jquery 的情况下使用此代码,它可以正常工作。无法理解为什么它在通过 jquery 移动后不起作用。

4

4 回答 4

2

鉴于已克隆的表单与之前的名称相同,当您尝试提交表单时,这会在 DOM 中产生错误。

建议:每次克隆时更改表单名称。

解决方案(jQuery):

var txtarea = $("#textarea_wrap").html();
$("#comment_10").html(txtarea);

var txtarea = $("#textarea_wrap").html();
$("#comment_10").html(txtarea).find('form').attr('name','post_comment'+(Math.floor(Math.random()*10000)));
于 2013-04-18T17:01:13.427 回答
0

不过不确定..检查是否可以像这样提交

$('.button').live('click',function({
    $('#give your form Id').submit();
});
于 2013-04-18T16:59:45.120 回答
0

这可能是问题的一部分,也可能不是问题的一部分,但是您的提交按钮定义中有语法错误。记得关闭 value 属性value="Submit Comment的引号

于 2013-04-18T17:05:08.240 回答
0

两种形式都具有相同的名称“post_comment”。表单名称应该是唯一的。

于 2013-04-18T17:03:40.187 回答