我有两个输入字段,fee和currency.已经工作:currency如果用户进入0该字段,我的字段将被禁用fee.我想是有外地currency禁用也当字段fee为空(这有时是默认值,但不总是).我的代码到目前为止:
$("#fee").on('input', function() {
var activeFee = (this.value === '0' || this.value === null ) ? true : false;
$('#currency').prop('disabled', activeFee);
});Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
或者:JSFiddle
代码中没有按预期工作的部分是:
(this.value === '0' || this.value === null )
Run Code Online (Sandbox Code Playgroud)
如何确保currency在两种情况下都禁用该字段是fee"0"还是空(空)?
空文本框的值是'',而不是null.此外,您可以手动触发事件侦听器,以便在代码运行时最初应用效果.
$("#fee")
.on('input', function() {
var activeFee = (this.value === '0' || this.value === '') ? true : false;
$('#currency').prop('disabled', activeFee);
})
.trigger('input');Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)