jQuery Datepicker:范围和内联

发布于 2024-11-13 23:17:42 字数 1083 浏览 2 评论 0原文

我想结合 jQuery UI Datepicker 来显示范围,并显示此内联。它具有两者的功能,但我无法将它们结合起来。它只是显示两个 div 中的范围,因此它无法按预期工作。这是代码:

JS:

<script type="text/javascript">
$(function(){
    var dates = $(" #to, #from" ).datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 2,
            onSelect: function( selectedDate ) {
                var option = this.id == "from" ? "minDate" : "maxDate",
                    instance = $( this ).data( "datepicker" ),
                    date = $.datepicker.parseDate(
                        instance.settings.dateFormat ||
                        $.datepicker._defaults.dateFormat,
                        selectedDate, instance.settings );
                dates.not( this ).datepicker( "option", option, date );
            }
        });
});
</script> 

HTML:

<div id="from"></div>
<div id="to"></div>

结果: 在此处输入图像描述

如您所见,有 4 个日历,并且它们单独工作,而不是仅仅工作两个月一起。

I want to combine the jQuery UI Datepicker to display both a range, and display this inline. It has functionality for both, but I am unable to combine them. It simply shows the range in BOTH divs so it is not working as expected. Here is the code:

JS:

<script type="text/javascript">
$(function(){
    var dates = $(" #to, #from" ).datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 2,
            onSelect: function( selectedDate ) {
                var option = this.id == "from" ? "minDate" : "maxDate",
                    instance = $( this ).data( "datepicker" ),
                    date = $.datepicker.parseDate(
                        instance.settings.dateFormat ||
                        $.datepicker._defaults.dateFormat,
                        selectedDate, instance.settings );
                dates.not( this ).datepicker( "option", option, date );
            }
        });
});
</script> 

HTML:

<div id="from"></div>
<div id="to"></div>

Result:
enter image description here

As you can see, there are 4 calendars and they work individually, instead of being just two months that work together.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛滥成性 2024-11-20 23:17:43

我们使用此脚本来进行日期范围选择。效果很好

we're using this script to have date range selection working. It works very well

美人如玉 2024-11-20 23:17:42

我遇到了同样的问题,并使用以下代码让它工作:

<script>$(function() {
var dates = $( "#from, #to" ).datepicker({
    dateFormat: 'yy-mm-dd',
    onSelect: function(dateText, inst) {
        //set value
        $("#" + this.id + "_value").val(dateText);
        //set the min or max date
        var option = this.id == "from" ? "minDate" : "maxDate",
        instance = $( this ).data( "datepicker" ),
        date = $.datepicker.parseDate(
            instance.settings.dateFormat ||
            $.datepicker._defaults.dateFormat,
            dateText, instance.settings );
        dates.not( this ).datepicker( "option", option, date );
    }
});});</script>

<div id="from" class="datepicker"></div><input type="hidden" id="from_value" name="from_value"/><BR><BR><BR><div id="to" class="datepicker"></div><input type="hidden" id="to_value" name="to_value"/>

祝你好运!

I had the same problem and got it to work with the following code:

<script>$(function() {
var dates = $( "#from, #to" ).datepicker({
    dateFormat: 'yy-mm-dd',
    onSelect: function(dateText, inst) {
        //set value
        $("#" + this.id + "_value").val(dateText);
        //set the min or max date
        var option = this.id == "from" ? "minDate" : "maxDate",
        instance = $( this ).data( "datepicker" ),
        date = $.datepicker.parseDate(
            instance.settings.dateFormat ||
            $.datepicker._defaults.dateFormat,
            dateText, instance.settings );
        dates.not( this ).datepicker( "option", option, date );
    }
});});</script>

<div id="from" class="datepicker"></div><input type="hidden" id="from_value" name="from_value"/><BR><BR><BR><div id="to" class="datepicker"></div><input type="hidden" id="to_value" name="to_value"/>

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文