javascript - 仅当文本输入为 0 时禁用按钮

标签 javascript html css

我有以下代码:

var total = document.getElementById('total--input');

document.getElementById('btn-increment-total').addEventListener('click', function() {
  if (total.value > 1) {
    console.log('enabled');
    document.getElementById('btn-decrement-total').enabled = true;
  }

  total.value++;
});

document.getElementById('btn-decrement-total').addEventListener('click', function() {
  if (total.value == 0) {
    console.log('disabled');
    document.getElementById('btn-decrement-total').disabled = true;
  }

  total.value--;
});
<button id="btn-increment-total">plus</button>
<button id="btn-decrement-total">min</button>
<input type="text" id="total--input" value="1">

“递减”按钮似乎可以工作,并且会在满足条件时自行禁用。

但是“递增”按钮似乎并没有重新启用“递减”按钮。任何人都知道为什么以及如何解决这个问题?

最佳答案

没有enabled属性,你应该使用disable="false" :

document.getElementById('btn-decrement-total').disabled = false;

代替:

document.getElementById('btn-decrement-total').enabled = true;
_______________________________________________^^^^^^^

工作样本:

var total = document.getElementById('total--input');

document.getElementById('btn-increment-total').addEventListener('click', function() {
  total.value++;

  if (total.value > 0) {
    console.log('enabled');
    document.getElementById('btn-decrement-total').disabled = false;
  }

});

document.getElementById('btn-decrement-total').addEventListener('click', function() {
  total.value--;

  if (total.value == 0) {
    console.log('disabled');
    document.getElementById('btn-decrement-total').disabled = true;
  }
});
<button id="btn-increment-total">plus</button>
<button id="btn-decrement-total">min</button>
<input type="text" id="total--input" value="1">

关于javascript - 仅当文本输入为 0 时禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52633532/

相关文章:

javascript - d3 .tsv 文件不起作用

jquery - 将下拉列表值传递给 Grails 中的 jquery

html - CSS - 已声明样式的计算样式

javascript - 带有 <img> 标签的图片库?

javascript - 点击不会触发jquery函数

javascript - include 语句在 Google 脚本中不起作用

javascript - Google Apps 脚本 - 可选的链接抛出 ParseError

javascript - facebook 是否为手机和桌面设计了不同的网页

CSS3 : Strange padding on bottom of an image

html - 如何让子元素显示在其父元素后面(较低的 z-index)?