假设我有一个输入框和一个标签,所以当我在输入框上输入内容时,标签会在我输入时自动更改。
例子:
//the result 4 is dynamically put in the label as a result of input value.
<label class="result">4</label>
//this is the input where i put a value of 2
<input type="text" class="amount" name="amount" value="2" onkeyup="compute()">
//this is the function that is called to perform the calc.
function compute()
{
var x = document.getElementsByClassName("amount");
document.getElementsByClassName("result").value = x.value + 2;
}
在上面的示例中,我输入了 2,结果作为计算结果立即输出到标签中。