jQuery - resizing form input based on value's width?

后端 未结 10 1369
挽巷
挽巷 2020-12-06 17:28

Is it possible to get input\'s value width and resize the input dynamically so the value will fit?

Here\'s an example:

http://jsfiddle.net/bzBdX/2/

10条回答
  •  渐次进展
    2020-12-06 17:55

    All those solutions based on number of characters are quite weak, because each character could be of a different width if it is not a monotype font. I'm solving this problem with creating a div containing value of text input with the same font properties and getting its width as a required one.

    Something like this:

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

提交回复
热议问题