Javascript 如何在没有提交按钮的情况下使数字输入动态?
假设账单金额输入不正确,我们在输入所有其他输入后才意识到。我们如何返回并更改输入的数字,同时使其余数字也随之更改。这里是一个链接到我的JS上的代码笔。我也在这篇文章上贴了一些,因为它迫使我这么做Javascript 如何在没有提交按钮的情况下使数字输入动态?,javascript,Javascript,假设账单金额输入不正确,我们在输入所有其他输入后才意识到。我们如何返回并更改输入的数字,同时使其余数字也随之更改。这里是一个链接到我的JS上的代码笔。我也在这篇文章上贴了一些,因为它迫使我这么做 tipForm.onchange = function() { people.oninput = function() { numberOfPeople.innerHTML = this.value; } percentage.oninput = fun
tipForm.onchange = function() {
people.oninput = function() {
numberOfPeople.innerHTML = this.value;
}
percentage.oninput = function() {
tipPercentage.innerHTML = this.value + '%';
}
bill.onchange = function() {
bill.value = this.value;
}
tip = (bill.value * (Number(percentage.value/100))).toFixed(2);
totalTip.innerHTML = '$' + tip;
total = Number(bill.value) + Number(bill.value) * (Number(percentage.value/100));
totalAmount.innerHTML = '$' + total;
split = (total / people.value).toFixed(2);
splitAmount.innerHTML = '$' + split;
}```
如果我正确理解了你的问题,你希望一个变量依赖于其他变量的值。我会把它变成一个函数,这样当它被使用时,值总是被计算出来的。我还建议不要在onchange函数中定义其他函数