簡體   English   中英

如何在jQuery UI Datepicker中調整其他輸入的大小?

[英]How to resize additional input in jquery ui Datepicker?

如何設置通過jQueryUI datepicker小部件獲取值的動態添加的input字段的寬度?

我有2個輸入,“日歷輸入”是我們選擇日期的地方,它將通過altField方法( 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/

你是這個意思嗎

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


如果您不想使用插件,則可能希望看到以下解決方案:

調整輸入字段的寬度為其輸入

當您只想對日期選擇器日期選擇做一些事情時,您可以將您的東西添加到日期選擇器的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);
        }
    });

這里是工作提琴 ,你在找什么。

基本輸入自動調整大小的插件(這只是延長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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM