1
从来就得到了这个工作代码:JS:为什么.value在一个案例中起作用,但在另一个案件中不起作用?
HTML
Aumentar un <input id="porcentaje">%
<input type="button" id="aumentar" value="Aumentar" onclick="calcular()">
<br>Monto: <input id="campo">
<br> If you insert a number into the "campo" input it should be aumented
<br>by the same aumont that says the "porcentaje" input.
JS
function calcular() {
var original = document.getElementById('campo');
var aumento = document.getElementById('porcentaje').value;
original.value = original.value*aumento/100;
}
我不明白什么是为什么这个JS代码的工作,而这一次不是:
function calcular() {
var original = document.getElementById('campo').value;
var aumento = document.getElementById('porcentaje').value;
original = original*aumento/100;
}
换句话说,为什么我可以使用var original = document.getElementById('campo');但它不适用于var original = document.getElementById('campo').value;? 但它确实与var aumento = document.getElementById('porcentaje')。值;。
我开始学习JS了,这种差别我不明白。 在这两种情况下,我都试图达到输入值,但在一种情况下它确实有效(var aumento),而在另一种情况下它不会(原始)。