简体   繁体   English

ajax 成功后更改文本区域的高度 function

[英]Change height of a textarea after ajax success function

There is a textarea in my code used to show the message responded from the backend.我的代码中有一个文本区域,用于显示从后端响应的消息。 And I want to increase the height automatically when the length of contents is so long that appearing a scrollbar.当内容的长度太长以至于出现滚动条时,我想自动增加高度。 here is my code:这是我的代码:

$.ajax({
    type: 'POST',
    async: false,
    url: '/text_translation',
    data: JSON.stringify(data),
    success: function(returnStr) {
        $.each(returnStr,function(item, value) {
            returnString.html(returnString.html() + value + '\n');//show message
        });

        console.log(returnString.clientHeight);//undefined

        //if message too long, increase the height 
        if (returnString.clientHeight < returnString.scrollHeight) {
            returnString.css('height', 'auto').css('height', returnString.scrollHeight + (returnString.offsetHeight - returnString.clientHeight));    
        }

}

At first, because ajax is asynchronous, I set the "async" as "false" to ensure the change can be executed after the each function, but it also can't work.一开始,因为ajax是异步的,所以我将“async”设置为“false”,保证每次function之后可以执行更改,但也无法正常工作。 I tried to print the clientHeight, it showed "undefined" no matter async was false or true.我尝试打印clientHeight,无论异步是假还是真,它都显示“未定义”。 Doesn't setting false mean that the statement I'm calling has to complete before the next statement in my function can be called?设置 false 是否意味着我正在调用的语句必须在我的 function 中的下一条语句可以调用之前完成? I've been confused to the result, which was a specific number I thought.我对结果感到困惑,这是我认为的特定数字。

And then I tried to use the "input" event function like this:然后我尝试像这样使用“输入”事件 function:

returnString.on('input',function() {
    if (this.clientHeight < thhis.scrollHeight) {
        $(this).css('height', 'auto').css('height', this.scrollHeight + (this.offsetHeight - this.clientHeight));
    }
});

Nothing changed.没有改变。

I must do something wrong, but I don't know where is it.我必须做错什么,但我不知道错在哪里。 Please tell me the reason, thanks for your answer first.请告诉我原因,先谢谢你的回答。

Have a look here for how async works with $.ajax, because you're using the success callback, I don't think it affects how your code works in this scenario. 在这里查看 async 如何与 $.ajax 一起工作,因为您使用的是成功回调,我认为它不会影响您的代码在这种情况下的工作方式。

To resize the text area, I found this answer useful and have created a function where you pass in the textarea element below.要调整文本区域的大小,我发现这个答案很有用,并创建了一个 function,您可以在其中传入下面的 textarea 元素。

 $("button").click(() => { let returnStr = "Test\n\nTest\n\nTest\n\nTest\n\nTest\n\nTest"; $("textarea").val(returnStr); resizeTextareaHeight($("textarea")[0]); }); function resizeTextareaHeight(textarea) { while ($(textarea).outerHeight() < textarea.scrollHeight + parseFloat($(textarea).css("borderTopWidth")) + parseFloat($(textarea).css("borderBottomWidth"))) { $(textarea).height($(textarea).height() + 1); }; }
 textarea { overflow-y: hidden; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <button type="button">Pretend Ajax Call</button> </div> <div> <textarea></textarea> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM