使用jquery问题将文本附加到文本区域 [英] Append text to text area with jquery issue

查看:104
本文介绍了使用jquery问题将文本附加到文本区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一些按钮用jquery将文本附加到文本区域,我有它工作,但只有当我不在textarea本身中输入任何内容时。

Im trying to make some buttons append text to a textarea with jquery, and I have it working, but only if I dont type anything into the textarea itself.

代码:

<textarea name="comments" id="comments" rows="20" style="margin-left: 0px; margin-right: 0px; width: 968px;"></textarea>
<div>
  <button>+petname</button>
  <button>+lastvisit</button>
  <button>+nextvisit</button>
</div>
<script>
$( "button" ).click(function() {
  var text = $( this ).text();
  $('#comments').append(text); 
});
</script>

此代码正常工作,但是当我在该文本区域输入其他内容时,按钮不再工作???为什么!!??我只是想不通。
非常感谢。
Jason

This code is working, but the minute I type something else into that text area, the buttons no longer work??? WHY!!?? I just cant figure it out. Much thanks. Jason

推荐答案

而不是追加 set val 使用其函数参数语法,这样做:

Instead of doing append set val using its function argument syntax, do this way:

$('#comments').val(function(_, val){
    return val + text; 
}); 

演示

Demo

这篇关于使用jquery问题将文本附加到文本区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆