我有问题,因为代码不能计算价值。
我写了11并且是支出:11,接下来我写了11并且是1111,但是我需要22所以我使用了minusValue.value + = a;
你知道哪里出错吗?
var minusCashField = document.getElementById('minusCashField');
minusCash.addEventListener("click", function() {
var a = minusCashField.value;
minusValue.value += a;
});

<div class="threeBox">
<div class="resultbutton">
<input class="minusValue" disabled id="minusValue" value=''>
</div>
</div>
</header>
<div class="addFunction">
<fieldset>
<label><strong>Expenditure:</strong></br></label>
<input placeholder="Enter Expenditure...." tabindex="2" required type="text" id="minusCashField">
<button type="submit" id="minusCash">Submit</button>
</fieldset>
</div>
&#13;
答案 0 :(得分:0)
答案 1 :(得分:0)
你需要将两个字符串转换成整数。
var minusCashField = document.getElementById('minusCashField');
minusCash.addEventListener("click", function() {
var a = parseInt(minusCashField.value || 0, 10);
minusValue.value = parseInt(minusValue.value || 0, 10) + a;
});
答案 2 :(得分:0)
由于这些输入的值将是字符串,因此您需要在执行任何数学运算之前将字符串转换为数字 - 您分配给a
的字符串值,以及结果中包含的值您添加a
。
minusCash.addEventListener("click", function() {
var a = Number(minusCashField.value);
minusValue.value = Number(minusValue.value) + a;
});
答案 3 :(得分:0)
使用parseInt()将字符串转换为整数。
var minusCashField = document.getElementById('minusCashField');
minusCash.addEventListener("click", function() {
var a = parseInt(minusCashField.value || 0, 10);
minusValue.value = parseInt(minusValue.value || 0, 10) + a;
});
<div class="threeBox">
<div class="resultbutton">
<input class="minusValue" disabled id="minusValue" value=''>
</div>
</div>
</header>
<div class="addFunction">
<fieldset>
<label><strong>Expenditure:</strong></br></label>
<input placeholder="Enter Expenditure...." tabindex="2" required type="text" id="minusCashField">
<button type="submit" id="minusCash">Submit</button>
</fieldset>
</div>