textarea 只读自动高度(填充选定日期)

textarea readonly auto height (that fills with selected dates)

我使用 MultiDatesPicker 日历,用户可以在其中 select 一些日期。文本区域(带有 readonly 标签)对象自动填充日期,而用户 selects 其中一些并且工作正常。我现在喜欢做的是使 textarea 的高度等于其中文本的高度(来自 Multidatespicker 的日期)。从第 1 行开始。

我试过这个答案(第二个答案)Creating a textarea with auto-resize (working: jsfiddle) 但对我的情况不起作用

HTML

<div class="pick-multi-dates"></div>
<textarea rows="1" id="input-multi-dates" class="input-multi-dates"></textarea>

JQUERY

    $('.pick-multi-dates').multiDatesPicker({
        minDate: 0,
        altField: '#input-multi-dates',
        onSelect: function() {
            $('.input-multi-dates').css('height', 'auto' );
            $('.input-multi-dates').height( this.scrollHeight );
        }
    });

CSS

.input-multi-dates {
    overflow-y: hidden; /* prevents scroll bar flash */
    padding-top: 1.1em; /* prevents text jump on Enter keypress */
}

谢谢

试试这个:

$('.pick-multi-dates').multiDatesPicker({
        minDate: 0,
        altField: '#input-multi-dates',
        onSelect: function() {
            var text = document.getElementById('input-multi-dates');
            text.style.height = 'auto';
            text.style.height = text.scrollHeight+'px';
        }
    });

或您的固定 jQuery 版本:

$('.pick-multi-dates').multiDatesPicker({
    minDate: 0,
    altField: '#input-multi-dates',
    onSelect: function() {
        var $area = $('.input-multi-dates');
        $area.css('height', 'auto' );
        // 'this' within onSelect function refers to the associated input field
        // see: http://api.jqueryui.com/datepicker/#option-onSelect
        $area.css( 'height', $area[0].scrollHeight );
        // according to jQuery doc append 'px' is not neccessary: When a number is passed as the value, jQuery will convert it to a string and add px to the end of that string.
        // see: http://api.jquery.com/css/
    }
});