简体   繁体   English

与带有/不带 value 属性的输入元素混淆

[英]confused with input element with/without value attribute

I'm not new to HTML but I always get confused with input elements, below is some html:我对 HTML 并不陌生,但我总是对输入元素感到困惑,下面是一些 html:

<input id="firstTest" />
<input id="secondTest" value="Hello"/>

so for the firstTest input, I typed some text like "Hi",then do:所以对于 firstTest 输入,我输入了一些文本,例如“Hi”,然后执行:

let firstInput = document.getElementById('firstTest');
console.log(firstInput.value)  //produces "Hi"

but for the secondTest input, I did the same thing but the value is always "Hello" and I can't type anything into the input field.但是对于 secondTest 输入,我做了同样的事情,但值始终是“Hello”,我无法在输入字段中输入任何内容。

So my questions are:所以我的问题是:

Q1-why for the firstInput, I didn't specify a value attribute but the value can change depending on what I typed? Q1-为什么对于 firstInput,我没有指定 value 属性,但 value 可以根据我输入的内容而改变?

Q2-when I type sth into the field, what actually happened? Q2-当我在字段中输入 sth 时,实际发生了什么? How browser display the thing I type?浏览器如何显示我输入的内容? does input object in DOM get its value property updated automatically then the browser displays the latest value on screen?在 DOM 中输入 object 是否会自动更新其value属性,然后浏览器会在屏幕上显示最新值?

Q3-if input object in DOM get its value property updated automatically, why on the secondInput that has a value attribute couldn't get its value property updated automatically? Q3-如果在DOM中输入object自动更新其value属性,为什么第二个有value属性的输入不能自动更新其value属性?

Q1-why for the firstInput, I didn't specify a value attribute but the value can change depending on what I typed? Q1-为什么对于 firstInput,我没有指定 value 属性,但 value 可以根据我输入的内容而改变?

The value HTML attribute serves only for the purpose of defining an initial value . value HTML属性用于定义初始值 input elements have a Javascript API, and in this, there is a value property (which you are using in your console.log). input元素有一个 Javascript API,其中有一个value属性(您在 console.log 中使用)。 This value property constantly reflects whatever is in the input .value属性不断反映input中的任何内容。 If you want to know what is in the value attribute, there's two ways to find out:如果您想知道 value 属性中的内容,有两种方法可以找到:

el.getAttribute('value');

or, using the aforementioned API或者,使用前面提到的 API

el.defaultValue;

Q2-when I type sth into the field, what actually happened? Q2-当我在字段中输入 sth 时,实际发生了什么? How browser display the thing I type?浏览器如何显示我输入的内容? does input object in DOM get its value property updated automatically then the browser displays the latest value on screen?在 DOM 中输入 object 是否会自动更新其 value 属性,然后浏览器会在屏幕上显示最新值?

That's a matter of how the browser handles it internally.这是浏览器如何在内部处理它的问题。 This is an implentation detail which is never relevant for the web developer.这是一个与 web 开发人员无关的实现细节。 It is laid out in the specification for that element.它在该元素的规范中列出。

Q3-if input object in DOM get its value property updated automatically, why on the secondInput that has a value attribute couldn't get its value property updated automatically? Q3-如果在DOM中输入object自动更新其value属性,为什么第二个有value属性的输入不能自动更新其value属性?

Given the code you show here, that cannot be the case.鉴于您在此处显示的代码,情况并非如此。 The only ways to disable typing in an input is if the input has a disabled or readonly attribute, or if someone has added a keydown listener that calls event.preventDefault() .禁用输入的唯一方法是input是否具有disabledreadonly属性,或者如果有人添加了调用event.preventDefault()keydown侦听器。

If this does not fully answer your question, please leave a comment stating what is remaining unclear or unanswered.如果这不能完全回答您的问题,请发表评论说明尚不清楚或未回答的问题。

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

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