首页 文章

如何在jquery ui Datepicker中调整其他输入的大小?

提问于
浏览
0

如何通过jQueryUI datepicker小部件设置动态添加的 input 字段的宽度?

我有2个输入,"Calendar input"是我们选择日期的地方,它将通过 altField 方法(datepicker的核心方法)自动添加到我们的附加输入中 . 那么我可以动态调整 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/

3 回答

  • 0

    这是您正在寻找的working fiddle .

    基本输入autoresize插件(只是扩展了one的isValidWidthChange限制):

    (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();
    
  • 0

    因为你只想在datepicker日期选择上做一些事情,你可以在日期选择器的 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);
            }
        });
    
  • 0

    你的意思是这样的吗?

    $("#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

相关问题