简体   繁体   English

如何根据值长度更改输入宽度

[英]How to change input width depending by value length

I made a simple bitcoin price checker and one user reported that the input field is not showing properly:我做了一个简单的比特币价格检查器,一位用户报告输入字段显示不正确:

这里

On my PC, Firefox and Chrome is working fine but for some reasons for others it may not look how it should:在我的 PC 上,Firefox 和 Chrome 运行良好,但由于某些原因,它可能看起来不应该如何:

这里

I also tried on mobile devices, my phone and my tablet and there are working fine as well.我还尝试了移动设备、手机和平板电脑,它们也都运行良好。 Right now I'm using JavaScript to determine the size attribute for input field but seems to not work properly for everybody.现在我正在使用 JavaScript 来确定输入字段的大小属性,但似乎并不适合所有人。 Another solution is with css and removing the size attribute.另一种解决方案是使用 css 并删除 size 属性。 Sience I have no experience with JavaScript how can I do it? Sience 我没有使用 JavaScript 的经验,我该怎么做?

My JavaScript我的 JavaScript

function refresh_price() {
    $.ajax({
        url:"query.php?currency=USD"
    }).done(function(data) {
        $("#bitcoin_value").attr("value", data).attr("size", data.length - 2).addClass("highlight");
        setTimeout(function() {
            $("#bitcoin_value").removeClass();
        }, 500);
    });
}
refresh_price();
setInterval(refresh_price, 5000);

HTML HTML

<input id="bitcoin_value" type="text" value="..." size="1" readonly>

EDIT编辑

Ok so I end up removing size attribute which was something that I was looking for and I switched my JavaScript to add the width without changing my JavaScript with something complicated from the answers to my question because I don't really understand them.好的,所以我最终删除了我正在寻找的 size 属性,并且我切换了我的 JavaScript 以添加宽度而不更改我的 JavaScript,因为我并不真正理解它们。 I added this.我加了这个。

.css("width", 15 + data.length * 16)

15 = the padding 15 = 填充

data.length = the number of my value output length. data.length = 我的值输出长度的数量。

16 = the number of pixels per character according to my font family and font size. 16 = 根据我的字体系列和字体大小,每个字符的像素数。

 function fitInputToVal(input) { var el = $('<span>').hide().appendTo(document.body); el.text(input.val() || input.text() || input.attr('placeholder')).css('font', input.css('font')); var inputWidth = el.width(); input.css({ width: inputWidth }) } $('#bitcoin_value').on('input', function() { fitInputToVal($(this)); }).trigger('input');
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" id="bitcoin_value" placeholder="val here"></input>

If you need to change width of that field you can do it by multiplying number of characters by some number.如果您需要更改该字段的宽度,您可以通过将字符数乘以某个数字来实现。 For example if you think one character can be 8 pixels wide, do it this way:例如,如果您认为一个字符可以是 8 像素宽,请这样做:

Please note that this example work only if variable data.length is returning length of price.请注意,此示例仅在变量data.length返回价格长度时才有效。

function refresh_price() {
    $.ajax({
        url:"query.php?currency=USD"
    }).done(function(data) {
        $("#bitcoin_value").attr("value", data).attr("size", data.length - 2).addClass("highlight");
        $("#bitcoin_value").css('width', (data.length * 8) + 'px');
        setTimeout(function() {
            $("#bitcoin_value").removeClass();
        }, 500);
    });
}
refresh_price();
setInterval(refresh_price, 5000);

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

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