【问题标题】:getElementById return null when apply it to select OptiongetElementById 在将其应用于选择选项时返回 null
【发布时间】:2020-08-24 02:10:39
【问题描述】:

在我的角度应用程序中,我正在尝试执行一些条件代码,当结果为真时,我想禁用包含动态对象列表的选择选项,当尝试获取此选择的 id 时,它返回 null !!

ngOnInit() {
    this.setScripts()
    this.http.get('api/cities').subscribe((cities: any[]) => {
      this.cities = cities;
    })

    this.http.get('api/user').subscribe(user => {
      this.currentUser = user
      localStorage.removeItem('currentUser')
      localStorage.setItem('currentUser', JSON.stringify(this.currentUser))

      this.user.patchValue({
        firstName: this.currentUser.firstName,
        lastName: this.currentUser.lastName,
        email: this.currentUser.email,
        profession: this.currentUser.profession,
        gender: this.currentUser.gender,
        birthDate: this.currentUser.birthDate,
        phone: this.currentUser.phone,
        oldPassword: this.currentUser.oldPassword,
        password: this.currentUser.newPassword,
        address: this.currentUser.address,
        postalCode: this.currentUser.postalCode,
        city: this.currentUser.city._id
      })
    })
    this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
    if (this.compareDatesByDays(new Date().toISOString(), JSON.parse(localStorage.currentUser).municipalityDateChange) < 10) {
      this.hideSelectMunicipality()
    }
  }



  hideSelectMunicipality(){
    $('#selectCity').prop('disabled', true);
   }

***html

  <div class="col-lg-7">
                  <select id="selectCity" name="city" class="custom-select form-control rounded" formControlName="city">
                    <option *ngFor="let city of cities" [value]="city._id" >{{city.name}}</option>
                  </select>
                </div>

【问题讨论】:

  • 您尝试将[disabled]="condition" 添加到您的&lt;select&gt; 吗?

标签: javascript html jquery angular select


【解决方案1】:

用这个,

hideSelectMunicipality(){
this.user.controls.city.disable();
}

【讨论】:

    【解决方案2】:

    我认为您当前用户的城市日期更改天数低于 10,因此执行了“hideSelectMunicipality”功能,请检查数据库中的用户详细信息。

    【讨论】:

      【解决方案3】:

      我建议为此目的使用 ngClass 并在某些情况下禁用它们

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-25
        • 1970-01-01
        • 2018-09-03
        • 1970-01-01
        • 2015-07-02
        • 2023-03-26
        • 1970-01-01
        • 2011-01-31
        相关资源
        最近更新 更多