用户输入在JavaScript中需要每次重新安排页面以更改值

发布于 2025-01-29 15:05:43 字数 582 浏览 3 评论 0原文

我构建一个从用户编号中获取的网站,并通过单击按钮执行特定的数学操作,例如用户类型“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆依然 2025-02-05 15:05:43

您不会从内存堆栈重置输入值,因此,当您尝试调用程序值时,您的程序将第一个存储的值用于该变量。
尝试在事件侦听器函数的情况下实例化输入变量,以便每次调用此操作时,您实际上都在变量中获取新值。

    <div id="result"></div>
    <input type="number" id="ID">
<button id="Button">15%</button>


const button = document.getElementById("Button");
button.addEventListener('click', ()=>{
 const input= document.getElementById("ID");
 const operation = input.value * 0.15;
 console.log(operation);
 document.getElementById("result").innerHTML=`${operation}`;
});

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.

    <div id="result"></div>
    <input type="number" id="ID">
<button id="Button">15%</button>


const button = document.getElementById("Button");
button.addEventListener('click', ()=>{
 const input= document.getElementById("ID");
 const operation = input.value * 0.15;
 console.log(operation);
 document.getElementById("result").innerHTML=`${operation}`;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文