gpt4 book ai didi

javascript - 如果其他字段为 '0' 或 null,则 jQuery 禁用字段

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:57:30 26 4
gpt4 key购买 nike

我有两个输入字段,feecurrency已在工作:如果用户在字段 fee 中输入 0,我的字段 currency 将被禁用。 我想要的是在 fee 字段为空(有时是默认值,但并非总是如此)。到目前为止我的代码:

$("#fee").on('input', function() {
var activeFee = (this.value === '0' || this.value === null ) ? true : false;
$('#currency').prop('disabled', activeFee);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Fee: <input id="fee"><br>
Currency: <input id="currency"><br>

或:JSFiddle

代码中没有按预期工作的部分是:

(this.value === '0' || this.value === null )

无论 fee 是 '0' 还是空 (null),如何确保字段 currency 被禁用?

最佳答案

空文本框的值为'',而不是null。此外,您可以手动触发事件监听器以在代码运行时最初应用效果。

$("#fee")
.on('input', function() {
var activeFee = (this.value === '0' || this.value === '') ? true : false;
$('#currency').prop('disabled', activeFee);
})
.trigger('input');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Fee: <input id="fee"><br> Currency: <input id="currency"><br>

关于javascript - 如果其他字段为 '0' 或 null,则 jQuery 禁用字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51520314/

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