修改“值”。 “文本”的属性输入无法使用JavaScript [英] Modifying "value" attribute of "text" input not working using JavaScript

查看:52
本文介绍了修改“值”。 “文本”的属性输入无法使用JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对文本输入字段有疑问,该字段的值已由JavaScript函数修改。这是我的代码的简化代码段:

I have a question regarding a text input field, whose value is modified by a JavaScript function. Here is a simplified snippet of my code:

<!doctype html>
<html>
    <meta charset="utf-8">
    <head>
        <title>Test Page</title>
        <script>
            function runScript()
            {
                document.getElementById("result").innerHTML = "Changing Text Input Value to Name 2";
                document.getElementById("name_input0").value = "Name 2";
                document.getElementById("name_hidden0").value = "Name 2";
            }
        </script>
    </head>
    <body>
    <input type="hidden" id="name_hidden0" value="Name 1"/>
    <input type="text" name="name_enter0" id="name_input0" value="Name 1"/>
    <input type="button" value="Run Script" onClick="runScript()"/>
    <div id="result"></div>
    </body>
</html>

页面加载时,文本和隐藏的输入均填充值名称1。当我单击按钮时,页面如下所示:

When the page loads, the text and hidden input are both populated with the value "Name 1". When I click the button, the page looks like this:

这是很有意义的,但是,如果我使用开发人员工具(尝试过Firefox和Chrome)查看源代码,在HTML代码中,我看到以下内容:

which makes perfect sense, however, if I look at the source code using the Developer Tools (I tried Firefox and Chrome) and look at the HTML code, I see the following:

为什么文本输入字段的值属性在基础HTML中没有变化,但是隐藏的输入值却没有变化? / p>

Why did the "value" attribute of the text input field not change in the underlying HTML, but the hidden input "value" did?

推荐答案

对于文本输入:

HTML value 属性设置默认值。

The HTML value attribute sets the default value.

DOM 属性保留 current 值。

The DOM value property holds the current value.

它们之间没有1:1映射。

There isn't a 1:1 mapping between them.

这篇关于修改“值”。 “文本”的属性输入无法使用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆