How do you set the default value of a jQuery DatePicker that is bound with KnockoutJS?
Using KnockoutJS for a jQuery datepicker found here, I have the following setup:
<span data-bind="text: myDateObject"></span>
<input style="display:none"
data-bind="enable: enabledBoolean,
datepicker: myDateObject,
datepickerOptions: {
buttonImage: '/Content/calendar.png',
buttonImageOnly: true,
showOn: 'button',
showOtherMonths: true,
selectOtherMonths: true,
defaultDate: new Date(),
minDate: '-1m',
maxDate: '+1m' }" />
myDateObject
is a javascript Date object that could be set to null. When I click on the calendar image and the datepicker pops up, though, it always defaults to the maximum date开发者_如何学编程, even if I have already brought up the datepicker and set a value once already.
As you can see in the code sample above, I'm trying to set the defaultDate to the current date, but it seems to ignore this completely. Perhaps this is due to the KnockoutJS bindings?
The issue ended up being related to how myDateObject
was being returned back for the datepicker to access. The custom ko.dependentObservable
being accessed was returning the date in a custom format, when it really needed access to the raw data.
To resolve the issue, another ko.dependentObservable
was created to allow direct access to the raw Date.
精彩评论