使用jQuery onChange中的Vue更新数据值

时间:2017-01-23 10:56:13

标签: javascript jquery ecmascript-6 vuejs2

我正在使用带Vue.js的bootstrap-date-picker 2. Bootstrap datepicker不会更新model

但如果我使用以下脚本,则无法访问范围以使用this

loadFormProps() {
// init other stuff
$('#founding_date').change(function() {
  this.founding_date = $(this).val();
});
}

模态如下:

data() {
  return {
    founding_date: '01 - 01 - 2017',
  }
};

最佳解决方案是什么,因为我无法使vm.$data正常工作,而且我无法在函数中访问this

1 个答案:

答案 0 :(得分:3)

您必须在this JS块之前将var保存在其他onchange内:

loadFormProps() {
// init other stuff
var that = this  
$('#founding_date').change(function() {
  that.founding_date = $(this).val();
});
}