【问题标题】:How to resize additional input in jquery ui Datepicker?如何在 jquery ui Datepicker 中调整附加输入的大小?
【发布时间】:2016-01-17 16:08:24
【问题描述】:

如何设置通过 jQueryUI 日期选择器小部件获取值的动态添加的 input 字段的宽度?

我有 2 个输入,“日历输入”是我们选择日期的地方,它将通过 altField 方法(日期选择器的核心方法)自动添加到我们的附加输入中。那么我可以动态调整input 的大小使其更小\更大吗?

Additional input: <input type="text" id="alternate" value="December,19 2015"><br>
Calendar input: <input type="text" id="from">    
$("#from").datepicker({
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 1,
    altField: "#alternate",
    altFormat: "MM d, yy",
    onClose: function (selectedDate) {
        $("#to").datepicker("option", "minDate", selectedDate);
    }
});

这是小提琴的链接 - https://jsfiddle.net/mhf7kxo4/

【问题讨论】:

  • 您想仅在通过日期选择器选择日期时调整输入的大小吗?
  • 是的。我的意思是附加输入将具有默认值并且它将被禁用,但是当用户通过日期选择器选择日期时附加输入的值将被更改并且还应该调整输入的宽度。

标签: javascript jquery html jquery-ui datepicker


【解决方案1】:

这是您正在寻找的working fiddle

基本输入自动调整大小插件(只是扩展了这个oneisValidWidthChange限制):

(function($){
    $.fn.autoResizeInput= function(o) {

    o = $.extend({
        maxWidth: 1000,
        minWidth: 0,
        comfortZone: 70
    }, o);

    this.filter('input:text').each(function(){

        var minWidth = o.minWidth || $(this).width(),
            val = '',
            input = $(this),
            testSubject = $('<tester/>').css({
                position: 'absolute',
                top: -9999,
                left: -9999,
                width: 'auto',
                fontSize: input.css('fontSize'),
                fontFamily: input.css('fontFamily'),
                fontWeight: input.css('fontWeight'),
                letterSpacing: input.css('letterSpacing'),
                whiteSpace: 'nowrap'
            }),
            check = function() {

                if (val === (val = input.val())) {return;}

                // Enter new content into testSubject
                var escaped = val.replace(/&/g, '&amp;').replace(/\s/g,'&nbsp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
                testSubject.html(escaped);

                // Calculate new width + whether to change
                var testerWidth = testSubject.width(),
                    newWidth = (testerWidth + o.comfortZone) >= minWidth ? testerWidth + o.comfortZone : minWidth,
                    currentWidth = input.width(),
                    isValidWidthChange = ( (newWidth < currentWidth || newWidth > currentWidth )&& newWidth >= minWidth)
                                         || (newWidth > minWidth && newWidth < o.maxWidth);

                // Animate width
                if (isValidWidthChange) {
                    input.width(newWidth);
                }

            };

        testSubject.insertAfter(input);

        $(this).bind('keyup keydown blur update', check);

    });

    return this;

    };
})(jQuery);

也只需添加这些行:

$("#from")
.datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        altField: "#alternate",
        altFormat: "MM d, yy",
        onClose: function (selectedDate) {
            $("#to").datepicker("option", "minDate", selectedDate);
            $("#alternate").focus().blur();
        }
    });
$("#alternate")
        .autoResizeInput({
            maxWidth: 18,
            minWidth: 11,
            comfortZone: 5
        })
        .focus()
        .blur();

【讨论】:

  • @NazárAtamaníuk 好的,我已将插件更正为自动调整大小而不仅仅是自动增长。干杯。
【解决方案2】:

由于您只想在日期选择器日期选择上做一些事情,您可以将您的内容添加到日期选择器的 onCloseonSelect 回调中

例子:

    $("#from").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        altField: "#alternate",
        altFormat: "MM d, yy",
        onClose: function (selectedDate) {
            // do you stuff over here
            $("#alternate").width(100).removeClass("disabled");

            $("#to").datepicker("option", "minDate", selectedDate);
        }
    });

【讨论】:

    【解决方案3】:

    你的意思是这样吗?

        $("#from").datepicker({
    
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        altField: "#alternate",
        altFormat: "MM d, yy",
        onClose: function (selectedDate) {
            $("#to").datepicker("option", "minDate", selectedDate);
            $('input').css('width','auto');
        }
    });
    

    jsFiddle Demo


    如果你不想使用插件,你可能希望看到这个解决方案:

    Adjust width of input field to its input

    【讨论】:

    • 是和否,我们不能像你那样设置输入的宽度 20px;它应该自动改变附加输入的宽度,例如如果用户选择日期 2015 年 12 月 19 日,它将调整该日期的附加输入,但如果用户选择 2015 年 6 月 1 日,宽度应在较小的一侧自动更改。你知道我的意思吗?这里是示例 jsfiddle.net/4Qsa8,但它通过用户编写的输入文本工作
    猜你喜欢
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多