Javascript 单击即可将文本添加到文本区域

Javascript 单击即可将文本添加到文本区域,javascript,jquery,forms,Javascript,Jquery,Forms,我得到了这个分数,它在大多数情况下都有效(jQuery是必需的): 说明 $(“#获取替代文本”)。单击(函数(){ $(“#文本”).text(“这是可选文本”).show(); }); 如果单击“使用替代文本”链接,则替代文本将被抛出到文本区域。但这只有在用户尚未将内容放入文本区域时才有效 我需要它工作,以便即使文本区域已经填充,然后单击“使用替代文本”链接将替换当前文本区域中的所有内容 任何建议都将不胜感激。用于设置textarea值,而不是.text(): .只需将文本更改为val

我得到了这个分数,它在大多数情况下都有效(jQuery是必需的):

说明


$(“#获取替代文本”)。单击(函数(){ $(“#文本”).text(“这是可选文本”).show(); });
如果单击“使用替代文本”链接,则替代文本将被抛出到文本区域。但这只有在用户尚未将内容放入文本区域时才有效

我需要它工作,以便即使文本区域已经填充,然后单击“使用替代文本”链接将替换当前文本区域中的所有内容

任何建议都将不胜感激。

用于设置
textarea
值,而不是
.text()


.

只需将
文本更改为
val

演示:


text区域没有值属性,请使用
text()
html()

演示:

jQuery:

$("#get-alternative-text").click(function(){
     $("#the-text").text("this is the alternative text");
});

我想应该是这样的

试试这个

$("#get-alternative-text").click(function(){
   var txt = $.trim($('#the-text').val());
   if(txt.length==0)
   {
     $("#the-text").attr('value', "this is the alternative text");
   }
 });

好极了!谢谢你。真不敢相信我忘了瓦尔。
$("#get-alternative-text").click(function () {
    $("#the-text").val("this is the alternative text").show();
});
$("#get-alternative-text").click(function(){
     $("#the-text").text("this is the alternative text");
});
 $(document).ready(function () {

    $("#get-alternative-text").click(function(){
     event.preventDefault();
     $("#the-text").val("this is the alternative text");
    });


 });
$("#get-alternative-text").click(function(){
   var txt = $.trim($('#the-text').val());
   if(txt.length==0)
   {
     $("#the-text").attr('value', "this is the alternative text");
   }
 });