Javascript 我应该使用哪种方法清理文本区域?

Javascript 我应该使用哪种方法清理文本区域?,javascript,jquery,ajax,html,Javascript,Jquery,Ajax,Html,我在文本区域中更改了一些文本,然后单击按钮,它将正常清理。但问题是,当我想向文本区域添加新数据时,它将不起作用,并且文本区域是空的 我应该使用哪种方法 $(".button10").on('click', function reset() { $.ajax({ type: 'GET', url:RestApi, success:function(data3){ var str = JSON.stringify

我在文本区域中更改了一些文本,然后单击按钮,它将正常清理。但问题是,当我想向文本区域添加新数据时,它将不起作用,并且文本区域是空的

我应该使用哪种方法

$(".button10").on('click', function reset() {    
    $.ajax({
        type: 'GET',
        url:RestApi,
        success:function(data3){
            var str = JSON.stringify(data3, undefined, 4);
            $("#myTextArea").html(str);
        },
    });
});

$(".button9").on('click', function reset() {
    $('#myTextArea').val('');
});

如果要将文本当前保留在文本区域中,请在ajax成功函数中使用
append
。使用
html
val
将用作为参数传递的文本替换所有文本:

$("#myTextArea").append(str);
如果要清除并替换文本区域中的文本,请使用
val
而不是
html
(cf answer from@Rushil Pachchigar):

试试这个:

$(".button10").on('click', function reset() {

$.ajax({
   type: 'GET',
    url:RestApi,
    success:function(data3){
    var str = JSON.stringify(data3, undefined, 4);
     $("#myTextArea").val(str);
      },
 });

});


$(".button9").on('click', function reset() {
        $('#myTextArea').val('');

});
试试这个

$(".button10").on('click', function reset() {

$.ajax({
type: 'GET',
url:RestApi,
success:function(str){
  $("#myTextArea").val(str);
},
}))

}))

使用
$('textarea').val(“”)


使用
$('textarea').text(“”)或
$('textarea').html(“”)
的问题在于,它只会删除服务器发送的原始DOM中的内容。如果用户将其清除,然后输入新的输入,则清除按钮将不再工作。使用.val(“”)可以正确地处理用户输入的大小写。

为什么不在ajax调用后清理文本区域而不是
.html()
时使用
.val()
呢?是否尝试了$('#myTextArea').val(null);
$(".button10").on('click', function reset() {

$.ajax({
type: 'GET',
url:RestApi,
success:function(str){
  $("#myTextArea").val(str);
},
$(".button9").on('click', function reset() {
    $('#myTextArea').val('');

});