javascript - 如果其他控件没有值,如何使复选框保持禁用状态,如果控件在 angular8 中有值则启用
问题描述
我有一个复选框字段,如果另一个控件有值,我想在其中启用复选框。这里我有 2 个控件amount and update received
。如果 update Received 控件中有值,则必须允许用户在金额字段中进行签入/签出。如果更新请求控件为空/null,则金额复选框必须保持禁用状态,并且不应允许用户签入/签出。
演示: 演示
TS:
initEoForm() {
this.eoInfoForm = this.FB.group({
amount: ['', Validators.required],
updateRequested:['']
});
}
public disableUpdate() {
let disabled = true;
if (this.eoInfoForm.value.updateRequested) {
disabled = false;
return false;
}
return disabled;
}
HTML:
<form [formGroup]="eoInfoForm">
<div class="row">
<div class="col">
<div class="custom-control custom-switch">
{{disableUpdate()}}
<input type="checkbox" class="custom-control-input" id="noNewBusiness"
formControlName="amount" disabled="disableUpdate()">
<label class="custom-control-label" for="noNewBusiness">Update Received</label>
</div>
</div>
</div>
</form>
为了查看我在 html 中得到的是真值还是假值,我尝试检查 value {{disableUpdate()}}
。它相应地给出真值或假值,但即使我得到真我也无法签入/签出。
解决方案
我稍微调整了一下,但我做到了。你需要 setAttribute() 和 removeAttribute()。我喜欢避免 if 块,我更喜欢三元运算符。
typing(event){
let checkBox = document.getElementById('noNewBusiness')
event.target.value == '' ? checkBox.setAttribute('disabled', 'true') : checkBox.removeAttribute('disabled')
}
推荐阅读
- angular - Agm Map 中的集群点击事件
- c++ - 如何使用 GDB 检查 c++ 映射中的单个元素并访问映射中元素指向的对象的成员
- vba - 在 MS Access VBA 中插入语句不适用于 CurrentDB Execute
- python - Matplotlib y 轴未排序
- data-science - 在谷歌趋势中搜索(可能是虚假的)相关性
- node.js - 从角度到 node.js API 的 for 循环中的 Http 请求返回 404
- sql - 如何创建从 gcp 项目到“默认”网络的服务网络创建
- java - 单击列表视图中的项目时片段崩溃
- php - 如何排列2个简单的数组数据
- android - 使用 Restlet 的反向代理