gpt4 book ai didi

javascript - 基于下拉选择问题禁用输入字段

转载 作者:行者123 更新时间:2023-12-02 14:54:47 25 4
gpt4 key购买 nike

<div class="form-group form-animate-text col-lg-6 col-xs-12 col-md-6" >
<select class="form-text" id="val_equipfc" name="val_equipfc" onChange="checkOption(this)" required>
<option value = "A">Yes</option>
<option value = "B">No</option>
</select>
<span class="bar"></span>
<label><span style="color:red">*</span>With Equipment?</span></label>
</div>

这是我选择的类,这是输入字段 div

<div class="form-group form-animate-text col-lg-6 col-xs-12 col-md-6" >
<input type="text" class="form-text" id="val_equipnofc" name="val_equipnofc">
<span class="bar"></span>
<label><span style="color:red">*</span>Number of Equipment/s</label>
</div>

当我在下拉菜单中单击“否”时,禁用输入文本字段的 jquery 是这样的:

function checkOption(obj) {
var input = document.getElementById("val_equipnofc");
input.disabled = obj.value == "B";
}

我想知道出了什么问题,当我搜索如何做到这一点时,我的代码与网上的代码类似,请告诉我出了什么问题。

最佳答案

首先,您似乎遇到了标记问题(请参阅问题评论)

然后我会用 JavaScript 添加事件监听器,以避免 JS 未加载的问题

document.getElementById ("val_equipfc").addEventListener ("change", ...)

并使用 this 关键字访问新值。

参见this fiddle

<小时/>

PS:如果只有“是/否”选项,为什么不使用复选框而不是下拉列表

关于javascript - 基于下拉选择问题禁用输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35913933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com