代码之家  ›  专栏  ›  技术社区  ›  Dirk J. Faber

如果其他字段为“0”或空,则jquery禁用字段

  •  3
  • Dirk J. Faber  · 技术社区  · 7 年前

    我有两个输入字段, fee currency . 已经开始工作: 我的领域 货币 如果用户输入 0 在场上 费用 . 我想要什么 就是拥有这个领域 货币 残疾人 当田野 费用 为空(有时是默认值,但不总是)。我的代码到目前为止:

    $("#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 )
    

    如何确保现场 货币 在两种情况下都被禁用 费用 是“0”还是空(空)?

    1 回复  |  直到 7 年前
        1
  •  5
  •   31piy    7 年前

    空文本框的值为 '' 而不是 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>