简体   繁体   English

如何在不提交的情况下不断存储输入值

[英]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.

相关问题 提交没有表单的输入值 - Submitting a value of input without form 如何在不提交的情况下将选定的下拉值存储到php变量? - How to store a selected dropdown value to a php variable without submitting? 如何在不提交表单的情况下获取输入控件的价值? - How to get value of input control outside of a form without submitting it? jQuery:如何在没有人提交的情况下找到输入框的值? - Jquery: how to find value of input box without the person submitting? 如何从输入中获取价值而不提交并从数据列表中进行搜索 - How to get value from input without submitting and search it from datalist 无需提交表单即可获取输入字段的值 - Get value of input field without submitting form 如何在不提交表格的情况下获取输入框值并将计算值放入其他输入框? - How to get input box value without submitting form and put calculated value in other input box? 如何在不刷新页面之前提交后检查输入类型编号的值 - how to check the value of input type number after submitting it before without page refresh 如何在不提交表单的情况下获取javascript中输入字段的值? - How can I get the value of an input field in javascript without submitting a form? 如何计算两个输入表单字段并将值放入另一个而不使用jquery提交表单? - How to calculate two input form fields and put the value in another without submitting the form using jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM