javascript - 更改选择字段后不显示输入字段
问题描述
一旦用户在下拉菜单中选择“取消”,输入字段应该出现给用户,以便用户可以输入他们取消的原因。
但它不起作用。
默认情况下,输入字段被禁用,并且不会向用户显示。
我尝试使用onselect
但无济于事。
HTML
<div>
<select placeholder="Status?" onselect="closeReasonAction" name="changeStatus" id="changeStatus" disabled>
<option value="Pending">Pending</option>
<option value="Quoted">Quoted</option>
<option value="Cancelled">Cancelled</option>
<option value="Confirmed">Confirmed</option>
</select>
<input name="closeReason" type="RRField" id="closeReason" style="margin:4px; padding:2px;">
</div>
if (document.getElementById('changeStatus').value != "Cancelled") {
document.getElementById("closeReason").style.opacity = 0;
document.getElementById("closeReason").disabled = true;
}
else if (document.getElementById('changeStatus').value == "Cancelled") {
document.getElementById("closeReason").style.opacity = 1;
document.getElementById("closeReason").disabled = false;
}
一旦用户单击取消,此处禁用的输入字段应该能够并出现供用户输入
解决方案
我认为您错过了onchange
更改选择时会发生的功能。onselect
将函数修改为onchange
函数并添加opacity:0
到输入样式中并使其disabled
初始。
function closeReasonAction() {
if (document.getElementById('changeStatus').value != "Cancelled") {
document.getElementById("closeReason").style.opacity = 0;
document.getElementById("closeReason").disabled = true;
}
else if (document.getElementById('changeStatus').value == "Cancelled") {
document.getElementById("closeReason").style.opacity = 1;
document.getElementById("closeReason").disabled = false;
}
}
<div>
<select placeholder="Status?" onchange="closeReasonAction()" name="changeStatus" id="changeStatus" >
<option value="Pending">Pending</option>
<option value="Quoted">Quoted</option>
<option value="Cancelled">Cancelled</option>
<option value="Confirmed">Confirmed</option>
</select>
<input name="closeReason" type="RRField" id="closeReason" style="margin:4px; padding:2px; opacity:0" disabled />
</div>
推荐阅读
- tomcat - Apache Tomcat:如何在 [重新] 部署期间防止 404 状态
- reactjs - 错误消息“shap 不是函数” - 我该怎么办?
- php - php-ews 设置标志(跟进)日期不适用于收件箱消息
- reactjs - 如何在从 Firestore 数据库获取数据时修复状态更新
- database - 何时加入树选择性 = 1?
- r - 将不同的 ggplots 放在一起,同时保持一个共同的 face wrap
- ssl - 对 sslCertificate.insert 的 REST 调用失败并显示“无法解析 SSL 证书”,但 gcloud 工作正常
- javascript - JavaScript数组初始化之前的“var”指的是列表的元素还是数组本身?
- python - Python 并行计算 - 独家新闻
- python - Pygame:平滑的图片分离