【问题标题】:Dynamically expand the height of a textarea when typing more then the max width输入大于最大宽度时动态扩展文本区域的高度
【发布时间】:2016-02-08 05:06:51
【问题描述】:

当输入超过文本区域元素的最大宽度时,如何水平扩展文本区域?我也不想看到水平滚动条。

渲染的html

<textarea name="CatchPhrase" class="input-validation-error" id="CatchPhrase" aria-invalid="true" aria-required="true" aria-describedby="CatchPhrase-error" rows="2" cols="20" data-val-required="The Catch Phrase field is required." data-val="true" data-val-maxlength-max="50"
data-val-maxlength="The field Catch Phrase must be a string or array type with a maximum length of '50'." htmlattributes="{ id = profileCatchPhrase, class = form-control }">I like yoga sd;lasdlk asdks';aksd'asd 'asd';las ';asd'lasd';las'dl as'dla'sdl'asld as'd;las'dl; a;sdlka;sdklasd ;alsd;lkasda ;alskd;lkasd ;lkasd;lk</textarea>

渲染 HTML 之前

<div class="form-group">
  @Html.LabelFor(model => model.CatchPhrase, htmlAttributes: new { @class = "control-label col-lg-2 col-md-2 col-sm-2 col-xs-2" })
  <div class="col-lg-10 col-md-10 col-sm-10 col-xs-10">
    @Html.TextAreaFor(model => model.CatchPhrase, new { htmlAttributes = new { id = "profileCatchPhrase", @class = "form-control" } }) @Html.ValidationMessageFor(model => model.CatchPhrase, "", new { @class = "text-danger" })
  </div>
</div>

【问题讨论】:

    标签: javascript jquery css asp.net-mvc razor


    【解决方案1】:

    试试这样的:

    $('#CatchPhrase').on('input', function() {
      $(this).outerHeight(38).outerHeight(this.scrollHeight);
    });
    

    见:https://stackoverflow.com/a/34513436/4613398

    jsfiddle:https://jsfiddle.net/3qmztmjj/

    【讨论】:

      【解决方案2】:

      如果我正确理解了您的问题并且您想增加文本区域的水平宽度,您可以使用 maxlength 检查文本字符并使用步骤来增加宽度。希望这个 sn-p 对你有帮助。

      $("#profileCatchPhrase").keypress(function() {
          if ($(this).val().length > $(this).attr("data-val-maxlength-max")) {
            var width = parseInt($(this).css("width").split("p")[0]) + 10;
            $(this).css("width", width + "px");
            $(this).attr("data-val-maxlength-max", parseInt($(this).attr("data-val-maxlength-max")) + 5);
          }
        });
      

      这里我使用了 10px 作为 icrement 步骤,你可以相应地调整它。

      【讨论】:

        【解决方案3】:

        好吧,我假设你想水平扩展 textarea 而不是垂直扩展,那么你可以尝试这样的事情:

        <textarea type="text" value="" placeholder="Autosize"></textarea>
        

        并编写一个简单的 javascript 函数,它会水平扩展:

        $('textarea').keyup(function () {
         // I'm assuming that 1 letter will expand the input by 10 pixels
         var oneLetterWidth = 10;
        
         // I'm also assuming that input will resize when at least eleven characters
         // are typed
         var minCharacters = 11;
         var len = $(this).val().length;
         if (len > minCharacters) {
             // increase width
             $(this).width(len * oneLetterWidth);
         } else {
             // restore minimal width;
             $(this).width(150);
         }
        });
        

        这将完美运行。您还可以设置 textarea 最大宽度,因为它应该在一段时间后停止扩展,因此只需将此 css 包含在您的 html 的 head 部分:

         textarea { 
         max-width: 400px;
         }
        

        【讨论】:

          【解决方案4】:

          使用 jquery 和 textarea 的 id 如下所示

          $(function() {
             $('#CatchPhrase').autogrow();
          });
          

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-03-12
          • 2017-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多