简体   繁体   English

HTML/JS:如何让一个输入日期改变另一个输入日期?

[英]HTML/JS: How to make an input date change another input date?

So I'm working on an Angular app and I have these two date input fields:所以我正在开发一个 Angular 应用程序,我有这两个日期输入字段:

<div  class="col-lg-3">
      <div> {{ dataInizioLabel }} </div>
      <input required type="datetime-local" name="dataInizio" id="dateInput"
        [ngModel]="serverEvent.dataInizio | date:'yyyy-MM-ddTHH:mm'"
        (ngModelChange)="manageDateChange($event, dataInizio)" #dataInizio="ngModel">
      <small class="text-danger" *ngIf="dataInizio.errors">Campo obbligatorio</small>
    </div>
    <div class="col-lg-3">
      <div> {{ dataFineLabel }} </div>
      <input required type="datetime-local" name="dataFine" [ngModel]="serverEvent.dataFine | date:'yyyy-MM-ddTHH:mm'" id="dataFine"
        (ngModelChange)="manageDateChange($event, dataFine)" #dataFine="ngModel">
      <small class="text-danger" *ngIf="dataFine.errors">Campo obbligatorio</small>
    </div>

manageDateChange() JS (incomplete): manageDateChange() JS(不完整):

 manageDateChange(date, inputField) {
if (date) {
  this.serverEvent[inputField.name] = new Date(date);
}
let inizio = new Date(date.val())
console.log(inizio)

} }

I can't find a way to make so the dataInizioLabel uptades the dataFineLabel field with his same date when it gets changed.我找不到一种方法来使 dataInizioLabel 在 dataFineLabel 字段发生更改时使用相同的日期更新它。 For example, if dataInizioLaber gets set to 24/04/2022, I'd need dataFineLabel to self update to the same date.例如,如果 dataInizioLaber 设置为 24/04/2022,我需要 dataFineLabel 自行更新到同一日期。

Also, console says date.val() can't work and gives an error, so I don't think that's the right way to get the date value from the input field.此外,控制台说 date.val() 无法工作并给出错误,所以我认为这不是从输入字段获取日期值的正确方法。

You might want to try to use a two-way binding on your ngModel like this:你可能想像这样在你的 ngModel 上使用双向绑定:

[(ngModel)]="serverEvent.dataInizio | date:'yyyy-MM-ddTHH:mm'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM