基本提示计算器 JavaScript



我最基本的小费计算器不起作用。我觉得我的逻辑是正确的,语法应该没问题,但我错过了一些东西,我已经花了很多时间在这上面。请帮忙。非常感谢。

<body>
<input type='number' placeholder='Total price' id='user-input'/>
<button id="calculator">Calculate the tip</button>
<h3 id='result'></h3>
<script>
var userInput = document.getElementById('user-input');
var button = document.getElementById('calculator');
var h3El = document.getElementById('result');
button.addEventListener('click', tipCalculator);
function tipCalculator(){
var tip = userInput * 0.1;
h3El.innerHTML = tip;
}
tipCalculator();
</script>

更改此内容

var userInput = document.getElementById('user-input');

对此

var userInput = document.getElementById('user-input').value;

最新更新