2012-02-17 48 views

回答

8

這是第四ËjQuery代碼:

$('input').each(function(){ 
var value = $(this).val(); 
var size = value.length; 
// playing with the size attribute 
//$(this).attr('size',size); 

// playing css width 
size = size*2; // average width of a char 
$(this).css('width',size*3); 

})​; 

http://jsfiddle.net/bzBdX/7/

+0

這不是在jsfiddle上工作,請考慮添加'keydown'功能來聽按鍵 – 2017-12-28 15:19:03

1

東西簡單:

$('#resizeme').keydown(function(){ // listen for keypresses 
var contents = $(this).val();  // get value 
var charlength = contents.length; // get number of chars 
newwidth = charlength*9;   // rough guesstimate of width of char 
$(this).css({width:newwidth});  // apply new width 
});​ 

你可以改變個人的喜好/文字大小

例乘數:http://jsfiddle.net/bzBdX/6/

1
$(function(){ 
    $("input").keyup(function(){ 
     $(this).stop().animate({ 
     width: $(this).val().length*7 
    },100)     
    }) 
})​ 
+0

'length * 7'? 你用過[this](http://xkcd.com/221/)方法來計算它嗎? – 2013-12-23 07:57:13

1

http://jsfiddle.net/bzBdX/11/

所以我通過 插入字母跨度由它試圖計算寬度的例子,計算有寬度

(function() { 
    var mDiv = $("<span />").text("M").appendTo("body"), 
     mSize = mDiv.width(); 
    mDiv.detach(); 

    var letterSize = function(s) { 
     var sDiv = $("<span />").text("M" + s + "M").appendTo("body"), 
      sSize = sDiv.width(); 

     sDiv.detach(); 

     return (sSize - (mSize * 2)) * 0.89; 
    }; 

    $("input[data-resise-me]").each(function() { 
     var minSize = $(this).width(); 

     var resizeInput = function() { 
      var calcSize = $.map($(this).val(), letterSize); 
      var inputSize = 0; 
      $.each(calcSize, function(i, v) { inputSize += v; }); 

      $(this).width(inputSize < minSize ? minSize : inputSize); 
     }; 

     $(this).on({ keydown: resizeInput, keyup: resizeInput }); 
    }); 
}()); 

這可能是一個更好的方法。

+1

爲什麼你乘以0.89? – 2013-09-06 13:51:34

0

這個例子中作爲在Chrome 25和Firefox 19.過測試(http://jsfiddle.net/9ezyz/

function input_update_size() 
{ 
    var value = $(this).val(); 
    var size = 12; 

    // Looking for font-size into the input 
    if (this.currentStyle) 
     size = this.currentStyle['font-size']; 
    else if (window.getComputedStyle) 
     size = document.defaultView.getComputedStyle(this,null).getPropertyValue('font-size'); 
    $(this).stop().animate({width:(parseInt(size)/2+1)*value.length},500); 
    //$(this).width((parseInt(size)/2+1)*value.length); 
} 

$('input') 
    .each(input_update_size) 
    .keyup(input_update_size); 
9

我有一個jQuery插件GitHub上:https://github.com/MartinF/jQuery.Autosize.Input

它反映該輸入的值,以便它可以計算實際長度而不是猜測或提到的其他方法。

這裏你可以看到一個活生生的例子:http://jsfiddle.net/mJMpw/6/

例子:

<input type="text" value="" placeholder="Autosize" data-autosize-input='{ "space": 40 }' /> 

input[type="data-autosize-input"] { 
    width: 90px; 
    min-width: 90px; 
    max-width: 300px; 
    transition: width 0.25s;  
} 

你只需要使用CSS來設置最小/最大寬度,如果你想有一個不錯的效果使用的寬度的過渡。

您可以將輸入元素的data-autosize-input屬性的空格/距離指定爲json符號中的值。

當然你也可以只使用jQuery

$("selector").autosizeInput(); 
1

初始化正如@ManseUK提及。這條線爲我工作。 JQuery version 1.8

$('#resizeme')。CSS({寬度:newwidth});

1

基於字符數量的所有這些解決方案都非常薄弱,因爲如果不是單體字體,每個字符的寬度可能不同。我正在使用相同的字體屬性創建一個包含文本輸入值的div並解決此問題,並獲取其所需的寬度。

事情是這樣的:

function resizeInput() { 
    $('body').append('<div class="new_width">'+$(this).val()+'</div>'); 
    $(this).css('width', $('.new_width').width()); 
    $('.new_width').remove() 
} 
$('input') 
    // event handler 
    .keyup(resizeInput) 
    // resize on page load 
    .each(resizeInput); 
4

目前的答案是WAAY複雜的。繼承人快速

$('#myinput').width($('#myinput').prop('scrollWidth')) 

添加上述輸入keydown/up/press事件是微不足道的。 鉻合金測試

+0

從技術上講,問題提到了jquery。但是,這是解決問題的最佳方法。 RGDS – 2017-02-20 14:43:57

相關問題