使用 jquery 问题将文本附加到文本区域 [英] Append text to text area with jquery issue
本文介绍了使用 jquery 问题将文本附加到文本区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 jquery 制作一些按钮将文本附加到文本区域,并且我可以正常工作,但前提是我没有在文本区域本身中输入任何内容.
代码:
此代码有效,但是当我在该文本区域中输入其他内容时,按钮不再起作用???为什么!!??我就是想不通.非常感谢.杰森
解决方案
不要用它的函数参数语法做 append
set val
,这样做:
$('#comments').val(function(_, val){返回值 + 文本;});
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.
Code:
<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>
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
解决方案
Instead of doing append
set val
using its function argument syntax, do this way:
$('#comments').val(function(_, val){
return val + text;
});
这篇关于使用 jquery 问题将文本附加到文本区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文