javascript - 即使 java 脚本应该启用它,文本框仍然处于禁用状态
问题描述
我在这里有一个很简单的问题,但它让我很头疼 -
document.getElementById("yourText").disabled = true;
document.getElementById('yourBox').onchange = function() {
document.getElementById('yourText').enabled = this.checked;
};
<div class="form-group">
<label asp-for="IsGate"></label>
<input type="checkbox" id="yourBox" asp-for="IsGate">
</div>
<div class="form-group">
<label asp-for="Condition"></label>
<input type="text" id="yourText" asp-for="Condition">
</div>
这应该在选中时启用文本框IsGate
,并在未选中时禁用它。在默认情况下将其设置为禁用后,即使更改了复选框,它也会保持不变。
这里出了什么问题?
解决方案
.enabled
不是有效属性。使用 always.disabled
和!this.checked
反转逻辑
const el_yourText = document.getElementById('yourText');
const el_yourBox = document.getElementById('yourBox');
el_yourText.disabled = true;
el_yourBox.addEventListener('change', function() {
el_yourText.disabled = !this.checked;
});
<div class="form-group">
<label asp-for="IsGate"></label>
<input type="checkbox" id="yourBox" asp-for="IsGate">
</div>
<div class="form-group">
<label asp-for="Condition"></label>
<input type="text" id="yourText" asp-for="Condition">
</div>
推荐阅读
- java - 无法在图像上显示图像
- docker - 在另一个容器的本地主机上发布端口
- xcode10.1 - Xcode 10.1 在故事板文件的版本编辑器中突出显示一行(或多行)
- unix - 通过 SOCKS 路由所有控制台流量
- c# - 使用 EF6 更新实体的某些属性而不加载整个实体
- encoding - CloudCDN 是否支持 Brotli 编码的服务?
- swift - 如何在 Swift 存储属性中创建协议及其扩展?
- mysql - Mariadb 加载,但几分钟后崩溃。从“快照”恢复服务器后发生
- jenkins - jenkinsfile 没有将 env 传递给 sh
- gitlab - 如何仅获取 Gitlab API 响应中的特定字段?