我有一个简单的“新闻文章”列表,我希望可以用于评论。我创建了一个“添加评论”按钮。如何使用jquery添加按钮之前的<textarea>
我希望按钮创建一个<form>
与<textarea>
紧接在点击按钮之前,并在点击按钮后立即创建一个“取消”按钮。 (我提出了这个问题,同时提出这个问题)
唯一剩下的就是我希望“取消”按钮删除新创建的<textarea>
和它自己点击。另外,如何防止“添加评论”按钮制作多个空白的“textareas”?
$(document).ready(function(){
\t $(".AddComment").click(function() {
\t $("<form><textarea name='ResearchUpdate' placeholder='Enter your comment here' rows='3' cols='40' class='Commentary'></textarea> </form>").insertBefore(this);
\t $("<button class='CancelComment'>Cancel</button>").insertAfter(this);
\t });
\t
\t $(".CancelComment").click(function(){
\t \t $(this).prev(".Commentary").remove();
\t \t $(this).remove();
\t });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>News Entries</title>
</head>
<body>
<h2><a href="#">Killer Rabbit On The Loose</a></h2>
<p>Date: 01/23/2015</p>
<p>Author: Bobert</p>
<p>Subject: Animals</p>
<p>Most Recent Comment: None Yet.</p>
<button class="AddComment">Add Comment</button>
<h2><a href="#">Porsche Unveils Ultra Fast Minivan</a></h2>
<p>Date:02/14/2015</p>
<p>Author: Jimmy</p>
<p>Subject: Cars</p>
<p>Most Recent Comment:</p>
<p>This is genius on Porsche's behalf! I can drop off the kids at school, go grocery shopping and still have time to go racing with my buddies. All without worrying that my emissions aren't falsified and polluting the world. --SemiProRacer997</p>
<button class="AddComment">Add Comment</button>
<h2> <a href="#">Apple Releases iPhone Inifity</a></h2>
<p>Date:03/11/2015</p>
<p>Author: Frank</p>
<p>Subject: Technology</p>
<p>Most Recent Comment:</p>
<button class="AddComment">Add Comment</button>
</body>
</html>
使用** **一个创建textarea的(一次性),比绑定提交事件。 – Vixed
我试图建立你的建议,因为最终我想通过ajax提交这些信息。我如何确保未创建多个textareas,并且最终如何在textarea中有文本时运行ajax? – wizkid
我在代码中使用了一个,以防止创建多个textarea。 ajax调用已经在代码中,只需将'postUrl.php'更改为需要接收数据的url即可。 – Vixed