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

Map*_*Dev 4 javascript jquery ecmascript-6 vuejs2

我在 Vue.js 2 中使用 bootstrap-date-picker。Bootstrap datepicker 不会更新模型

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

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

模态如下:

data() {
  return {
    founding_date: '01 - 01 - 2017',
  }
};
Run Code Online (Sandbox Code Playgroud)

什么是最好的解决方案,因为我无法vm.$data工作,也无法this在函数内访问。

Sau*_*abh 6

你必须在JS 块之前保存this一些其他的内部:varonchange

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

  • 有那么容易吗?谢谢你,先生! (2认同)