简体   繁体   English

用jQuery调整textarea的高度

[英]resize height of textarea with jquery

I am try to duplicate fb like comment box , where they resize as text fills it using Expanding Text Areas Made Elegant 我尝试复制fb之类的注释框,在此它们使用调整为优雅的扩展文本区域来调整文本大小

My view looks like: 我的看法如下:

<div class='expandingArea'>
    <pre><span></span></pre>
    <textarea id="comment_body" name="comment[body]" placeholder="In my opinion...">
    </textarea>
</div>
<input class="comment_submit" name="commit" type="submit" value="Create Comment" />

my jquery is: 我的jQuery是:

$( document ).ready(function() {
  $('div.expandingArea').each(function() {
    var area = $('textarea', $(this));
    var span = $('span', $(this));
    area.bind('input', function() {
      span.text(area.val());
      $(".comment_submit").css({"margin": "0.25em 0"});
      $(".expandingArea.active textarea").css({"padding": "5px", "height": "95%"});
    });
    span.text(area.val());
    $(this).addClass('active');
  });
});

As user fills in data, the text area height increases and everything is fine. 当用户填写数据时,文本区域的高度会增加,一切都很好。 Then to submit he clicks on submit button, which makes an ajax call, entered data gets stored in database and I reset the content of text area with: 然后要提交,他单击submit按钮,该按钮将进行ajax调用,输入的数据将存储在数据库中,然后使用以下命令重置文本区域的内容:

$('textarea').val('');

But I need to set the height of text area back to it's origin height(When user enter text, its height was increased as text filled it). 但是我需要将文本区域的高度设置回其原始高度(当用户输入文本时,其高度会随着文本的填充而增加)。 I can't use something like: 我不能使用类似:

$('textarea').height(10);

As that will add height = 10px to element style of textarea and then my resizing of textarea when user enters data in textarea won't work.Does anyone know how can I undo that resizing of textarea? 因为那样会给textarea的元素样式增加height = 10px ,然后当用户在textarea中输入数据时我无法调整textarea的大小,有人知道如何撤销textarea的大小调整吗?

Further, if user types something in textarea after submitting then textarea resizes to normal size on it's own. 此外,如果用户在提交后在textarea中键入内容,则textarea会自行调整为正常大小。 I guess that triggers input binded with textarea. 我猜这会触发与textarea绑定的input So faking user input sort of thing can also work, but I don't know how to do it either. 因此,伪造用户输入也可以,但我也不知道该怎么做。

try: 尝试:

$( document ).ready(function() {
  $('div.expandingArea').each(function() {
    var area = $('textarea', $(this));
    var span = $('span', $(this));
    area.bind('input', function() {
      span.text(area.val());
      area.attr('rows', 10);  
    });
    span.text(area.val());
    $(this).addClass('active');
  });

  $('.comment_submit').click(function() {        
     $('#comment_body').attr('rows', 2).val('');
  });
});

JSFiddle: http://jsfiddle.net/ronqt8mc/ JSFiddle: http : //jsfiddle.net/ronqt8mc/

You bind the function that does the resizing on the input event. 您绑定对input事件进行大小调整的函数。 When you empty the textarea it updates the value, but the event isn't triggered yet. 当您清空文本区域时,它将更新该值,但尚未触发该事件。 One way to force the event is: 强制事件的一种方法是:

$('textarea').val('').trigger('input');

Example: jsfiddle 示例: jsfiddle

$("#comment_body").css("height", "50px"); // 50px - your default size.

谢谢阿德里亚诺·戈多伊

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

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