简体   繁体   English

更新“值”=属性<input>在更改 UI 中的值时或在按下输入时

[英]Update "value"= attribute of <input> on changing the value in UI or When enter Pressed

I have an input field of text type in my HTML file.我的 HTML 文件中有一个text类型的input字段。 It Doesn't belong to any form .它不属于任何form So i was actually trying to give input there and later take care of the input (In my case, make a POST request with ajax) by let's say collecting .value from these input field by referring it with document.getElementById() .But apparently the value i enter or edit on the UI there is not being "saved" in value attribute.所以我实际上是想在那里提供输入,然后处理输入(在我的例子中,使用 ajax 发出POST请求),让我们说通过使用document.getElementById()引用它从这些input字段中收集.value 。但显然我在 UI 上输入或编辑的值没有在value属性中“保存”。 As a result i am not being able to fetch those values later from js.结果我以后无法从 js 中获取这些值。 So is there any way for "saving" ui edited values in value attribute?那么有什么方法可以在value属性中“保存”ui 编辑的值吗?

You don't provide any code, but this snippet worked for me.您不提供任何代码,但此代码段对我有用。

 function clicked(){ var el = document.getElementById('input'); console.log(el.value); }
 <input type="text" id="input"/> <button onclick="clicked()">click</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM