如何添加<text区域>使用jquery的before按钮

How to add <textarea> before button with jquery

本文关键字:使用 gt jquery 按钮 before 区域 text 添加 何添加 lt      更新时间:2023-09-26

我有一个简单的"新闻文章"列表,我希望可以对其进行评论。我创建了一个"添加评论"按钮。

我希望该按钮在单击按钮之前立即创建一个带有<textarea><form>,并在单击按钮之后立即创建"取消"按钮。(我在提出这个问题时发现了这一点)

唯一剩下的是,我希望"取消"按钮在单击时删除新创建的<textarea>及其本身。对于额外的信用,我如何防止"添加评论"按钮产生多个空白的"文本区域"?

$(document).ready(function(){
	$(".AddComment").click(function () {
	    $("<form><textarea name='ResearchUpdate' placeholder='Enter your comment here' rows='3' cols='40' class='Commentary'></textarea> </form>").insertBefore(this);
	    $("<button class='CancelComment'>Cancel</button>").insertAfter(this);
	});
	
	$(".CancelComment").click(function (){
		$(this).prev(".Commentary").remove();
		$(this).remove();
	});
});
<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>

<script type="text/javascript">
    $('.AddComment').one('click',function(e){
        e.preventDefault();
        var bt=$(this);
        var el=$('<textarea name="comment" />');
        el.insertBefore(this);
        bt.text('Send comment').on('click',function(e){
            if (el.val().length==0){
                alert("Please fill the comment before send.");
                el.focus(); //Delete this row if you don't want user to focus on the textarea.
                return false;
            }
            $.ajax({   
                type: 'POST',
                data : el,
                cache: false,  
                url: 'postUrl.php',
                success: function(data){
                    bt.after($('<b>Comment sent</b>').hide().fadeIn());
                    bt.add(el).hide();
                }   
            })
        })
    })
</script>

显示和隐藏已经存在的项比动态创建并插入它们要好。

没有使添加评论超过一个文本区域的额外信用:

$(".AddComment").click(function () {
    $("<form><textarea name='ResearchUpdate' placeholder='Enter your comment here' rows='3' cols='40' class='Commentary'></textarea> </form>").insertBefore(this);
    $("<button class='CancelComment'>Cancel</button>").insertAfter(this);
    // Remove the click event and disable the button.
    $(this).off("click").prop("disabled", true);
});

我提出的解决方案:

确保使用类.comment-boxstyle="display: none;"加载每个帖子的注释框,并使用以下事件处理程序。

$(".AddComment").click(function () {
  $(this).prev(".comment-box").toggle();
});

此外,您可以使用cancel按钮来具有以下事件处理程序:

$(".cancel").click(function () {
  $(this).closest(".AddComment").hide();
});

最好的解决方案是用css类将容器div中的每个post项包装起来,我们稍后可以使用css类进行jQuery选择。

<div class="postItem">
  <h2><a href="#">Killer Rabbit On The Loose</a></h2>
  <p>Date: 01/23/2015</p>
  <p>Author: Bobert</p>    
  <button class="AddComment">Add Comment</button>
</div>
<div class="postItem">
  <h2><a href="#">Second post</a></h2>
  <p>Date: 01/23/2015</p>
  <p>Author: Shyju</p>    
  <button class="AddComment">Add Comment</button>
</div>

在取消按钮点击事件中,获取容器div,找到表单和取消按钮并将其删除,

$(document).on("click",".CancelComment",function (e){
    var _this = $(this);
    _this.closest(".postItem").find("form").remove();
    _this.remove();       
});

此处为工作样品

您需要使用jQueryon方法来绑定取消按钮上的点击事件,因为它们是由代码动态创建并注入DOM的。

我会把表单放在那里,但要把它隐藏起来,这样当你点击按钮时它就会出现。然后可以使用$('#buttonId').prop('disabled', true); 禁用该按钮

这将阻止任何进一步的点击,尽管点击无论如何都不重要,因为你只有一个表单可以显示,所以它不会再弹出。

单击取消按钮应启动一个功能,删除有问题的文本区域的内容,隐藏表单/文本区域,并重新启用评论按钮

show_comment:

function show_comment(){<br>
&nbsp;&nbsp;&nbsp;&nbsp;$('#formId').show();<br>
&nbsp;&nbsp;&nbsp;&nbsp;$('#cancelButton').show();<br>
&nbsp;&nbsp;&nbsp;&nbsp;$('#commentButton').prop('disabled', true)<br>
}

remove_comment:

function remove_comment(){<br>
&nbsp;&nbsp;&nbsp;&nbsp;$('#textareaId').html('');<br>
&nbsp;&nbsp;&nbsp;&nbsp;$('#commentButton').prop('disabled', false);<br>
&nbsp;&nbsp;&nbsp;&nbsp;$('#cancelButton').hide();
}

代码中的问题。

  1. 您正在为页面加载中不存在的取消按钮添加单击功能。添加取消按钮后,您必须添加单击
  2. 您正在对取消按钮执行prev()操作,这是错误的。取消按钮的上一个同级按钮是添加注释按钮,而不是文本区域

请查看以下代码是否修复了您的问题。我还禁用了添加按钮,一旦添加了评论文本区域,这样你就不会添加更多的文本区域

$(document).ready(function(){
	$(".AddComment").click(function () {
            var $addCommentBtn = $(this), 
                $commentTextArea;
            $addCommentBtn.prop( "disabled", true );
	    $commentTextArea = $("<form><textarea name='ResearchUpdate' placeholder='Enter your comment here' rows='3' cols='40' class='Commentary'></textarea> </form>").insertBefore(this);
	    $("<button class='CancelComment'>Cancel</button>").insertAfter(this).click(function (){
		$commentTextArea.remove();
		$(this).remove();
                $addCommentBtn.prop( "disabled", false );
	    });
	});
});
<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>