设置输入字段的默认值
如何在 JavaScript 中设置表单 文本字段的默认值?
How would you set the default value of a form <input>
text field in JavaScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在 JavaScript 中设置表单 文本字段的默认值?
How would you set the default value of a form <input>
text field in JavaScript?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(20)
这是一种方法:
This is one way of doing it:
我使用setAttribute():
I use
setAttribute()
:如果您的表单包含这样的输入字段
,您可以在 javascript 中编写代码,如下所示,将其值设置为
if your form contains an input field like
then you can write the code in javascript as given below to set its value as
您可以在不同情况下使用
document.querySelector()
,而不是使用document.getElementById()
来自另一个 Stack Overflow 答案的更多信息:
用
getElementById
和getElementsByClassName
EXAMPLE:或
Test:
Instead of using
document.getElementById()
you can usedocument.querySelector()
for different casesmore info from another Stack Overflow answer:
EXAMPLE:
or
Test:
如果您使用多种形式,您可以使用:
If you are using multiple forms, you can use:
试试这些。
Try out these.
答案非常简单
或者如果您想完全避免 JavaScript:您可以仅使用 HTML 定义它
The answer is really simple
Or if you want to avoid JavaScript entirely: You can define it just using HTML
这是使用
jQuery
的解决方案:或者使用
JavaScript
:Here is the solution using
jQuery
:Or, using
JavaScript
:简单的答案不是在 Javascript 中,获取占位符的最简单方法是通过 placeholder 属性
The simple answer is not in Javascript the simplest way to get the placeholder is through the place holder attribute
或
或
or
or
很简单;一个例子是:
It's simple; An example is:
如果该字段由于某种原因只有名称属性而没有其他属性,您可以尝试以下操作:
If the field for whatever reason only has a name attribute and nothing else, you can try this:
您还可以将默认值更改为新值
You can also change the default value to a new value
这部分你在html中使用
这是javaScript:
This part you use in html
This is javaScript:
直接访问
如果使用ID,那么您可以直接访问JS全局范围内的输入
Direct access
If you use ID then you have direct access to input in JS global scope
这里发生的事情比大多数答案所暗示的要多得多,也比问题让您期望的要多得多。让我们深入探讨并阐明这个问题。
设置输入元素的默认值:
value
与defaultValue
、属性与属性了解
value
属性之间的区别非常重要 和值
属性(有时也称为IDL 属性)在使用 HTML 和 JavaScript 中的输入元素时。这两者有不同的用途,了解它们的作用将使您能够有效地管理输入字段的初始值和当前值。1. value 属性
value
属性主要用于在浏览器首次解析 HTML 时设置输入元素的初始值 。该初始值也称为默认值。此属性对应的 JavaScript 属性不是
value
,而是defaultValue
。本质上,defaultValue
属性是value
属性的反映:这里,
this
指的是输入元素。正如您所看到的,value
属性和defaultValue
属性始终保持同步。但是,有一个警告:如果用户尚未与输入字段进行交互,则通过
setAttribute
以编程方式设置value
属性也会更新输入的当前值,这由value
属性表示。一旦用户修改输入字段,这种同步就会停止。2. value 属性
value
属性表示输入字段的当前值。如果输入字段具有初始值(通过 HTML 中的
value
属性以声明方式设置)并且用户修改此值、value
属性和value
属性将不再同步。一旦输入字段是“脏的”(即,用户修改过的),设置value属性将不再影响当前值。3.
value
属性与defaultValue
属性与value
属性 - 在实际代码中此代码示例演示了
value
的行为> 属性、defaultValue
属性和value
属性。尝试使用不同的按钮,观察日志输出,并注意手动修改输入字段的值后行为如何变化。There's quite a bit more going on here than most of the answers suggest, and also than the question would make you expect. Let's dive in and shed some light on the matter.
Setting the Default Value of an Input Element:
value
vsdefaultValue
, attribute vs propertyIt's important to understand the distinction between the
value
attribute and thevalue
property (also sometimes called IDL attribute) when working with input elements in HTML and JavaScript. These two serve different purposes and understanding their roles will allow you to effectively manage the initial and current values of an input field.1. The value Attribute
The
value
attribute is primarily used to set the initial value of an input element when the HTML is first parsed by the browser. This initial value is also known as the default value.The corresponding JavaScript property for this attribute is not
value
, but ratherdefaultValue
. Essentially, thedefaultValue
property is a reflection of thevalue
attribute:Here,
this
refers to the input element. As you can see, thevalue
attribute and thedefaultValue
property are always in sync.However, there's a caveat: if the user has not yet interacted with the input field, programmatically setting the
value
attribute viasetAttribute
will also update the current value of the input, which is represented by thevalue
property. This synchronization stops as soon as the user modifies the input field.2. The value Property
The
value
property represents the current value of the input field.If the input field had an initial value (set declaratively via the
value
attribute, in the HTML) and the user modifies this value, thevalue
attribute and thevalue
property will no longer be in sync. Once the input field is "dirty" (i.e., user-modified), setting the value attribute will no longer affect the current value.3.
value
attribute vsdefaultValue
property vsvalue
property - in real codeThis code example demonstrates the behavior of the
value
attribute,defaultValue
property, andvalue
property. Experiment with the different buttons, observe the log output, and note how the behavior changes once you manually modify the input field's value.您还可以尝试:
You can also try:
HTML:
JS:
HTML:
JS:
下面的代码工作得很好:
The following code work perfectly well: