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

后端 未结 10 1321
挽巷
挽巷 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:39

    I have a jQuery plugin on GitHub: https://github.com/MartinF/jQuery.Autosize.Input

    It mirrors the value of the input so it can calculate the actual length instead of guessing or other approaches mentioned.

    You can see an live example here: http://jsfiddle.net/jw9oqz0e/

    Example:

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

    You just use css to set min/max-width and use a transition on the width if you want a nice effect.

    You can specify the space / distance to the end as the value in json notation for the data-autosize-input attribute on the input element.

    Of course you can also just initialize it using jQuery

    $("selector").autosizeInput();
    

提交回复
热议问题