2

我想将 startDatePicker 与 endDatePicker 链接,有没有办法将它们与 Knockout.js 链接?

当用户选择一个新的开始日期时,endDatePicker 将被更新,将 minDate 设置为新的开始日期,并且关于新日期是否在其自己的日期之后,它是否也会更新当前日期?

在此先感谢,我正在尝试使用自定义活页夹,但我无法得到任何工作。

4

2 回答 2

2

您可以使用subscribe订阅可观察对象的更改。文档在这里: http: //knockoutjs.com/documentation/observables.html#explicitly_subscribing_to_observables

var ViewModel = function() {
   var self = this;
   self.startDatePicker = ko.observable();
   self.endDatePicker = ko.observable();

   self.startDatePicker.subscribe(function(newValue) {
       // set the value on the other one
   });

   self.endDatePicker.subscribe(function(newValue) {
       // set the value on the other one
   });
}; 

其余的取决于您的绑定...

于 2013-02-08T21:17:48.420 回答
2

That code should work, when selecting a minimum date it will then set the minDate of the second datepicker to its value, vice versa for max.

$('#min').datepicker({
    onSelect: function( selectedDate ) {
    $( "#max" ).datepicker( "option", "minDate", selectedDate );
    }
})

$('#max').datepicker({
    onSelect: function( selectedDate ) {
    $( "#min" ).datepicker( "option", "maxDate", selectedDate );
    }
})
于 2013-02-06T15:14:56.423 回答