[英]How to constantly store input value without submitting
I'm trying to make something where you can input numbers into an input (or would a contenteditable div be better?) and it does some calculations to them and sets the content of another div as the answer.我正在尝试制作一些可以在输入中输入数字的东西(或者一个可编辑的 div 会更好吗?),它对它们进行一些计算并将另一个 div 的内容设置为答案。 How can I make it so that the other div will update whenever the number changes?
我怎样才能让它在数字改变时另一个 div 会更新? I would prefer to make it so that there isn't a submit button so the number instantly changes as you type it.
我宁愿这样做,以便没有提交按钮,以便在您键入时数字会立即更改。 I could probably achieve this by updating it every time there is a keystroke but the problem is storing the input data without pressing a submit button
我可以通过每次击键时更新它来实现这一点,但问题是在不按提交按钮的情况下存储输入数据
First, please take some time to format your questions in the future.首先,请花一些时间在将来格式化您的问题。 This is a difficult read.
这是一个困难的阅读。
Second please try and include all the information to show how far you have gotten.其次,请尝试包含所有信息以显示您已经取得了多远。
Also I wasn't really clear what you are trying to do.我也不太清楚你要做什么。
What you are looking for is called two way data binding, there are many different options for it, but it doesn't mean that's what you need.您正在寻找的称为双向数据绑定,它有许多不同的选项,但这并不意味着这就是您所需要的。 It seems like the following would be a good solution for you.
似乎以下对您来说是一个很好的解决方案。
function doSomeCalculationsAndDisplay(event){ const elem = document.getElementById('input'); console.log(elem.value); document.getElementById('result').innerText = 'Double value: ' + parseFloat(elem.value) * 2; }
<input id="input" type="number" onKeyUp="doSomeCalculationsAndDisplay()" onChange="doSomeCalculationsAndDisplay()"> <div id="result"></div>
I always seem to figure it out on my own as soon as I make a post about it.我似乎总是在我发表一篇关于它的帖子时自己弄清楚。 It's not very complicated, the problem I had is that I was trying to get the innerHTML of an input which isn't possible, I needed to get its value instead.
这不是很复杂,我遇到的问题是我试图获取不可能的输入的innerHTML,我需要获取它的值。
To accomplish this all you need to do is make an input, div, and function that sets the div as the input document.getElementById('test1').innerHTML = document.getElementById('input1').value;
要完成此操作,您需要做的就是创建一个输入、div 和将 div 设置为输入的函数
document.getElementById('test1').innerHTML = document.getElementById('input1').value;
and call this function onkeyup in the input.并在输入中调用此函数 onkeyup。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.