简体   繁体   English

jQuery UI Datepicker日期范围

[英]jQuery UI Datepicker Date Range

I am using the jQuery UI plugin with the Datepicker function, to set a date range. 我正在使用带有Datepicker函数的jQuery UI插件来设置日期范围。 The example provided on their page ( http://jqueryui.com/demos/datepicker/date-range.html ) sets the range based on input 'id'; 他们页面( http://jqueryui.com/demos/datepicker/date-range.html )上提供的示例根据输入'id'设置范围; however, I'd like to set the range based on 'class' as my form 'clones' the div to add additional inputs, making the 'id' fields unique on each clone. 但是,我想将基于“类”的范围设置为表单“克隆” div以添加其他输入,从而使“ id”字段在每个克隆上都是唯一的。 When I change the JavaScript to use 'class' instead of 'id', the ranges are no longer functioning. 当我更改JavaScript以使用“类”而不是“ id”时,范围不再起作用。

JavaScript: JavaScript:

<script src="../../scripts/jquery-1.6.4.js"></script>
<script src="../../scripts/jqueryui/ui/jquery.ui.core.js"></script>
<script src="../../scripts/jqueryui/ui/jquery.ui.datepicker.js"></script>
<script>
        $(function() {
            var dates = $( ".start_date, .end_date" ).datepicker({
                onSelect: function( selectedDate ) {
                    var option = this.class == "start_date" ? "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: HTML:

<div>
    <label> Start Date:</label>
    <input type="text" name="start_date1" id="start_date1" class="start_date" />
</div>
<div>
    <label> End Date:</label>
    <input type="text" name="end_date1" id="end_date1" class="end_date" />
</div>

To check to see if your element has a class, do this: 要检查您的元素是否有一个类,请执行以下操作:

 if ($(this).is('.start_date')) {

or 要么

 if ($(this).hasClass('start_date')) {

The name of the property of DOM elements is "className", not "class" (confusingly). DOM元素的属性名称是“ className”,而不是“ class”(令人困惑)。 Also it makes your code fragile to check for the class name to be equal to some string, because you never know if you might want to add another class (like "required" for example). 同样,它使代码很难检查类名是否等于某个字符串,因为您永远都不知道是否要添加另一个类(例如“ required”)。 Thus you should always check either with jQuery or with some other method that accounts for the possibility that an object can have many classes. 因此,您应该始终使用jQuery或其他方法检查某个对象可能具有多个类的可能性。

Also you may want to check out the Filament Group's date range picker for jQuery. 另外,您可能想查看Filament Group的jQuery 日期范围选择器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM