如何在 Meteor 中提交和保存 bootstrap-datepicker 的值?

How to submit and save value from bootstrap-datepicker in Meteor?

我正在 Meteor 中制作应用程序原型,但在使用日期选择器时遇到问题。我在 Meteorpad.

中创建了一个最小示例

如何在单击时从日期选择器中获取选定的值并将其保存到数据库?我的活动代码在 client/app/js 的第 8 行。我使用 input 标签作为日期选择器的一部分,但由于没有 submit 按钮,我不确定我是否真的在更改日期。 onchange 在这里工作吗?

Template.Valuation.events({
  'onchange #selectedDate': function(e) {
    e.preventDefault();
    var valuationId = this._id;
    var selectedDate = $(e.target).find('[name=selectedDate]').val();
    Valuations.update(valuationId, {$set: {valuationDate: selectedDate}}, function () {
      });
  }
});

Does onchange work here?

不,不是。您将需要监听 changeDate events in your Template.Valuation.onRendered 函数:

Template.Valuation.onRendered(function() {
  $('#valuation .input-group.date').datepicker({
    todayBtn: "linked",
    orientation: "top auto",
    daysOfWeekDisabled: "0,6",
    todayHighlight: true,
    autoclose: true,
    format: 'yyyy-mm-dd'
  });
  var self = this;
  $('#valuation .input-group.date').datepicker().on('changeDate', function(ev) {
    var valuationId = self.data._id;
    var selectedDate = $('#selectedDate').val();
    Valuations.update(valuationId, {
      $set: {
        valuationDate: selectedDate
      }
    }, function() {});
  });
});

我在您的 valuationPrice 模板助手中也发现了一个错误。这可能会解决它:

valuationPrice: function() {
  var valuation = Valuations.findOne({
    _id: this._id
  });
  var valuationDate = valuation.valuationDate;
  var valuationPrice = 0;
  _.each(valuation.closingPrices, function(closingPrice) {
    if (closingPrice.date == valuationDate) valuationPrice = closingPrice.close;
  });
  return valuationPrice;
}

这是更新后的 MeteorPad