javascript - 如何清除文本区域不使用 .val ('' )

标签 javascript jquery

$('button').click(function(){
    $(this).parent().before('<div class="comment_box_all"><div class="comment_user"></div></div>')
    var content = document.createElement("span");
   content.innerHTML=$(".textarea").val().replace(/(\n|\r|\r\n)/g, '<br>');
    $('.comment_user').append(content);
});
$(document).on("click", "button", function() {
	$(this).closest('.comment_panel').find('.textarea').val('')
});
 $('button').attr('disabled',true);
   $('.textarea').keyup(function(){
      if($(this).val().length !=0)
          $('button').attr('disabled', false);            
       else
          $('button').attr('disabled',true);
 });
.comment_panel
{
  width:450px;
  height:100px;
}
textarea
{
  width:300px;
  height:80px;
  }
button
{
  
  top:10px;
  left:330px;
 }
.comment_box_all
{
  width:450px;
  height:100px;
  background-color:#999;
  border:1px solid #000;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="comment_panel">
    <textarea class="textarea" placeholder="Add text..."></textarea>
    <button>Add comment</button>  
</div>

你能帮我如何在不使用 .val('') 单击按钮后清除 textarea,因为看,当 textarea 为空时,我想禁用按钮,但这只发生在第一次添加文本时,如果我接下来添加文本textarea 中的时间为 '' 并且按钮处于事件状态

最佳答案

每次单击 add 按钮时,您都可以像第一次一样添加禁用类。

$(this).attr('disabled',true);

在按钮 onclick 函数中添加这一行。

$('button').click(function(){
    $(this).parent().before('<div class="comment_box_all"><div class="comment_user"></div></div>')
    var content = document.createElement("span");
   content.innerHTML=$(".textarea").val().replace(/(\n|\r|\r\n)/g, '<br>');
    $('.comment_user').append(content);
});
$(document).on("click", "button", function() {
	$(this).closest('.comment_panel').find('.textarea').val('')
    $(this).attr('disabled',true);
});
 $('button').attr('disabled',true);
   $('.textarea').keyup(function(){
      if($(this).val().trim().length !=0)
          $('button').attr('disabled', false);            
       else
          $('button').attr('disabled',true);
 });
.comment_panel
{
  width:450px;
  height:100px;
}
textarea
{
  width:300px;
  height:80px;
  }
button
{
  
  top:10px;
  left:330px;
 }
.comment_box_all
{
  width:450px;
  height:100px;
  background-color:#999;
  border:1px solid #000;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="comment_panel">
    <textarea class="textarea" placeholder="Add text..."></textarea>
    <button>Add comment</button>  
</div>

关于javascript - 如何清除文本区域不使用 .val ('' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41665375/

相关文章:

javascript - 更改提交的表单中的值和 JQuery 序列化函数

javascript - jQuery 自动完成 - 在 IE 中显示焦点值列表

javascript - 返回函数的函数

javascript - 如何使用 URI 使用 phonegap 压缩图像

javascript - Ajax Response 200 ok,但显示加载响应数据失败

javascript - 链接表行和列 - jquery

jquery - 如何根据rails查询ajax中的其他下拉列表值更改选择框值

javascript - div 内的元素不应干扰事件处理程序

javascript - 如何在 JSON 字符串中包含 Javascript 字符串构造函数?

javascript - 如何将 cookie 中存储的数字限制为小数点后 2 位?