用户输入在JavaScript中需要每次重新安排页面以更改值
我构建一个从用户编号中获取的网站,并通过单击按钮执行特定的数学操作,例如用户类型“ 100”,并在15%按钮处单击该应用程序将在控制台日志15中打印在第15页中,现在用户更改上一个值在输入字段至'80,按相同的按钮15%,它应该打印“ 12”,但不仍然是相同的结果'15',如果我想要结果,我需要重新加载页面,我该如何使结果每次更改时间用户更改JavaScript中的输入值, 这里的简单代码以获取更多详细信息:
html:
<input type="number" id="ID">
<button id="Button">15%</button>
javaScript:
const input= document.getElementById("ID");
const button = document.getElementById("Button");
button.addEventListener('click', ()=>{
const operation = input.value * 0.15;
console.log(operation);
});
I build a web site that take from the user number and perform specific math operations by clicking in buttons, for example user type '100' and click at 15% button the app will print in the console log 15 now the user change the previous value at the input field to '80 and press the same button 15% it should print '12' but no it still the same result '15' if I want the result I need to reload the page, how can I make the result change every time the user change the input value in javascript,
here simple code for more detail:
HTML:
<input type="number" id="ID">
<button id="Button">15%</button>
JAVASCRIPT:
const input= document.getElementById("ID");
const button = document.getElementById("Button");
button.addEventListener('click', ()=>{
const operation = input.value * 0.15;
console.log(operation);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不会从内存堆栈重置输入值,因此,当您尝试调用程序值时,您的程序将第一个存储的值用于该变量。
尝试在事件侦听器函数的情况下实例化输入变量,以便每次调用此操作时,您实际上都在变量中获取新值。
You're not resetting the input value from the memory stack, so when you try to call the variable value your program uses the first stored value to that variable.
Try to instantiate your input variable in the event listener function so that each time you call this operation you are actually taking the new value in your variable.