在附加输入旁边拉伸 HTML 输入框?

发布于 2024-12-26 16:22:45 字数 707 浏览 1 评论 0原文

我在这个网站上使用 jQuery,它有表单输入。我希望当用户输入数据并且空间耗尽时,一个特定字段变得更长(宽度)。

如何计算何时实际上需要使输入 (type="text") 更长? 对一种浏览器有效的方法可能无效适用于所有浏览器。

这是无法计算的东西,所以每个人都根据每个浏览器中的试验和错误来进行计算吗?我是否需要诉诸于此并使用 .keyup() 来检查拉伸值价值?..例如..

$(".stretchInput").keyup( function(event) {
   var someValueAdjustedByTrialAndError = 30;
   var stretchPastLength = someValueAdjustedByTrialAndError;
   var stretchBy = 5;
   var baseWidth = 100;
   if ($(this).val().length > stretchPastLength ) {
      $(this).css('width',(baseWidth + ($(this).val().length - stretchPastLength ) * stretchBy ) + 'px');
   } else {
      $(this).css('width','');
   }
});

I'm using jQuery on this site, which has form inputs. I'd like one particular field to get longer (width) as the user enters data and space runs out.

How can I calculate when I ACTUALLY need to make the input (type="text") longer? What works for one browser may not work for all browsers.

Is this something that can't be calculated, so everybody does it based on trial and error within each browser? Will I need to resort to this and tweak stretch values with a .keyup() that checks the value?.. EG..

$(".stretchInput").keyup( function(event) {
   var someValueAdjustedByTrialAndError = 30;
   var stretchPastLength = someValueAdjustedByTrialAndError;
   var stretchBy = 5;
   var baseWidth = 100;
   if ($(this).val().length > stretchPastLength ) {
      $(this).css('width',(baseWidth + ($(this).val().length - stretchPastLength ) * stretchBy ) + 'px');
   } else {
      $(this).css('width','');
   }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你没皮卡萌 2025-01-02 16:22:45

[已更新以检测退格键]

在页面上以及 keyup 事件中放置一个隐藏的 元素字段,您可以镜像跨度中的输入值。更新输入字段宽度以匹配您的跨度。

Html:

<span style="display:none"></span>
<input type="text" style="width:100px"; />

注意输入元素上的min-width值,该值是为了防止宽度缩小到某个值以下。

jQuery:

$('input').keyup(function() {
    var $span = $('span');
    var $this = $(this);
    $span.text($this.val()); 
    $this.width($span.width());
});

[UPDATED to detect backspace]

Place a hidden <span> element on the page, and within the keyup event of your <input> field you can mirror the input value in your span. Update the input field width to match your span.

Html:

<span style="display:none"></span>
<input type="text" style="width:100px"; />

Note the min-width value on the input element that is there to prevent the width from shrinking below a certain value.

jQuery:

$('input').keyup(function() {
    var $span = $('span');
    var $this = $(this);
    $span.text($this.val()); 
    $this.width($span.width());
});
[旋木] 2025-01-02 16:22:45

TextAreas 解决方案

描述

这听起来像是您想要一个像 Facebook 上一样的自动增长的文本区域。如果是这样
为什么要重新发明轮子
我鼓励您使用精彩的 Eternity jQuery 插件

弹性使您的文本区域增大和缩小以适应其内容。它的灵感来自 Facebook 上自动增长的文本区域。 Elastic 与其竞争对手之间的主要区别在于它的重量。

更多信息

文本输入解决方案

这是我针对 input 元素的解决方案。

问题是每个字体上的字符宽度并不是均匀

如果你选择courier作为字体,就可以让它变得完美。

示例

Html

<input class="stretchInput" style="font-family: courier"/>

jQuery

var initialWidth = $(".stretchInput").width();
$(".stretchInput").keyup( function(event) {
   var charWidth = 8.5;
   var width = $(this).val().length*charWidth;
   if ($(this).width() < width)
   {
       $(this).width(width);
   } else {
       $(this).width(initialWidth );
   }
});

查看我的 jsFiddle

Solution for TextAreas

Description

This sounds like you want a auto growing textarea like on facebook. If so
why reinvent the wheel ?
I encorage you to use the wonderful Elasting jQuery Plugin.

Elastic makes your textareas grow and shrink to fit it’s content. It was inspired by the auto growing textareas on Facebook. The major difference between Elastic and it’s competitors is it’s weight.

More Information

Solution for Text Inputs

Here my solution for input elements.

The problem is that the width of a char is not evenly on every font.

If you choose courier as the font, you can make it perfect.

Sample

Html

<input class="stretchInput" style="font-family: courier"/>

jQuery

var initialWidth = $(".stretchInput").width();
$(".stretchInput").keyup( function(event) {
   var charWidth = 8.5;
   var width = $(this).val().length*charWidth;
   if ($(this).width() < width)
   {
       $(this).width(width);
   } else {
       $(this).width(initialWidth );
   }
});

Check out my jsFiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文