我想使用bootstrap datepicker并将所选日期与knockoutjs绑定。
使用datepicker的函数:
$(function() {
// create the departure date
$('#depart-date').datepicker({
autoclose: true,
format: 'yyyy/mm/dd',
}).on('changeDate', function(ev) {
ConfigureReturnDate();
});
$('#return-date').datepicker({
autoclose: true,
format: 'yyyy/mm/dd',
startDate: $('#depart-date').val()
});
// Set the min date on page load
ConfigureReturnDate();
// Resets the min date of the return date
function ConfigureReturnDate() {
$('#return-date').datepicker('setStartDate', $('#depart-date').val());
}
});
这是我想要使用的小提琴,但不知道如何去做。 http://jsfiddle.net/zNbUT/276/
答案 0 :(得分:5)
我找到了一个可以帮助我的小提琴 http://jsfiddle.net/jearles/HLVfA/6/
小提琴的功能:
ko.bindingHandlers.datepicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
//initialize datepicker with some optional options
var options = allBindingsAccessor().datepickerOptions || {};
$(element).datepicker(options).on("changeDate", function (ev) {
var observable = valueAccessor();
observable(ev.date);
});
},
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
$(element).datepicker("setValue", value);
}
};
答案 1 :(得分:3)
我也使用bootstrap-datepicker.js,但方式不同:
My Viewmodel:
var MyDataViewModel = {
//Set Todays Date
StartDate: ko.observable(new Date())
}
我的HTML:
<div id="dtpDate" class="input-append">
<input required="required" id="txtdtpDate" data-format="yyyy-MM-dd" type="text" style="width: 75%;" />
<span class="add-on"><i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
JS让它发挥作用:
$(function () {
$('#dtpDate').datetimepicker({
pickTime: false
})
.on('changeDate', function (ev) {
//Date.Subtring(1,10) for formatting purposes
MyDataViewModel.StartDate(ko.toJSON(ev.date).substr(1, 10));
});
});
});
这对我来说非常适合