【问题标题】:Input Field Resize, Text Creeps after certain length输入字段调整大小,文本在一定长度后爬行
【发布时间】:2018-04-18 15:44:54
【问题描述】:

我正在创建一个类似于命令提示符的输入字段。作为查找输入字段的视觉提示,我在输入框的侧面有括号。当用户键入时,只要输入一个新字符,输入字段就会调整大小。

唯一的问题是,当输入过多时,输入字段的左侧会超出字段的范围。

这是输入字段的代码:

<div class="line" id="command-wrapper">
    <span class="text-green">[</span><input id="command" autofocus contenteditable="true" size=1/><span class="text-green">]</span>
    <button id="submit"></button>
</div>

这是重新计算输入字段大小的代码:

$('#command').keypress(function(e){
  if(e.which == 13){
    $('#submit').click();
  }
  $('#command').attr({'size': $('#command').val().length + 1});
});

这是我的 JSfiddle: https://jsfiddle.net/mediocreb/8anw08k1/6/

在该示例中,如果您键入超过 17 个字符,您将看到效果。在我的测试中,它在移动设备上更糟糕,通常只用几个字符就“爬行”。

【问题讨论】:

    标签: jquery css input


    【解决方案1】:

    我看过你的代码。它不会超出输入字段的范围。你必须用 textarea 替换 INPUT

    <div class="line" id="command-wrapper">
    <span class="text-green">[</span><textarea id="command" autofocus size=1> 
    </textarea><span class="text-green">]</span>
    <button id="submit"></button>
    </div>
    

    在 Jquery 中不需要更改

    像这样添加CSS

    textarea
    {
     overflow:hidden;
     width:80%;
     border:none;
    }
    

    【讨论】:

    • textarea 不是这样工作的。虽然我曾考虑使用textarea,但这需要对我的代码进行相当充分的检查,特别是因为我必须考虑 jQuery 中的“高度”和“宽度”值。不过,谢谢!
    猜你喜欢
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多