防止 var 变为负值
Prevent var from going negative
我有一个带有以 0 开头的 var 的按钮,当您单击另一个按钮时它会增加到 1。但是如果您先单击第二个按钮,则 var 变为负数。该按钮在开始时是禁用的,但是当它增加到 1 时应该启用该按钮。
当页面加载时按钮被禁用,但是当 var 变为 1 时按钮仍然被禁用。
我试过了:if(var == 0){document.getElementById("button1").disabled = true; return false;}
您可以像下面的代码片段那样做:
You can use any tag instead of input
like div
span
h1
... which ever you wish and style according to need .
This thing you will need to change will be change .value
to .innerHTML
everywhere like this(1 example) :
var units = document.getElementsByClassName("unitsMain")[0].innerHTML
function decreaseUnits() {
var units = document.getElementsByClassName("unitsMain")[0].value
if (units > 0) {
units--;
document.getElementsByClassName("unitsMain")[0].value = units;
}
}
function increaseUnits() {
var units = document.getElementsByClassName("unitsMain")[0].value
units++;
document.getElementsByClassName("unitsMain")[0].value = units;
}
<button class="decOrderUnits" onclick="decreaseUnits()"><i class="fa fa-minus"></i>-</button>
<input class="unitsMain" value="0" maxlength="3">
<button class="incOrderUnits" onclick="increaseUnits()"><i class="fa fa-plus"></i>+</button>
我有一个带有以 0 开头的 var 的按钮,当您单击另一个按钮时它会增加到 1。但是如果您先单击第二个按钮,则 var 变为负数。该按钮在开始时是禁用的,但是当它增加到 1 时应该启用该按钮。
当页面加载时按钮被禁用,但是当 var 变为 1 时按钮仍然被禁用。
我试过了:if(var == 0){document.getElementById("button1").disabled = true; return false;}
您可以像下面的代码片段那样做:
You can use any tag instead of
input
likediv
span
h1
... which ever you wish and style according to need .
This thing you will need to change will be change.value
to.innerHTML
everywhere like this(1 example) :
var units = document.getElementsByClassName("unitsMain")[0].innerHTML
function decreaseUnits() {
var units = document.getElementsByClassName("unitsMain")[0].value
if (units > 0) {
units--;
document.getElementsByClassName("unitsMain")[0].value = units;
}
}
function increaseUnits() {
var units = document.getElementsByClassName("unitsMain")[0].value
units++;
document.getElementsByClassName("unitsMain")[0].value = units;
}
<button class="decOrderUnits" onclick="decreaseUnits()"><i class="fa fa-minus"></i>-</button>
<input class="unitsMain" value="0" maxlength="3">
<button class="incOrderUnits" onclick="increaseUnits()"><i class="fa fa-plus"></i>+</button>