如果我设置`le Let x = document.getElementById('inputText" quot; quot; quot value'并更新'x`,为什么do nor do doal noce Update not doal the Value Update?

发布于 2025-01-18 15:33:52 字数 574 浏览 4 评论 0原文

在下面的示例中,为什么value输入的属性 test 更新为“ second”

document.getElementById("test").addEventListener("click", () => {
  let test = document.getElementById("test").value;
  
  test = "second";
  console.log(test); // Logs "second", but input value is not updated.
});
<label>Click on this test input: <input type="text" id="test" value="first"></label>

In the following example, why doesn’t the value property of the input with the ID test update to "second"?

document.getElementById("test").addEventListener("click", () => {
  let test = document.getElementById("test").value;
  
  test = "second";
  console.log(test); // Logs "second", but input value is not updated.
});
<label>Click on this test input: <input type="text" id="test" value="first"></label>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

篱下浅笙歌 2025-01-25 15:33:53

尽管JavaScript最终将所有内容都视为对象,但我相信只有阵列和对象是通过引用传递的。字符串,INT和浮子按价值传递。

文本输入将始终为您提供一个字符串(即使您将输入限制为数字)

Although javascript ultimately treats everything as an object, I believe only arrays and objects are passed by reference. Strings, ints and floats are passed by value.

Text inputs will always give you a string (even if you restrict input to numbers)

峩卟喜欢 2025-01-25 15:33:53
<script type="text/javascript">
    getText = function() {      
        var test = document.getElementById('test').value;
        test = "second";
        //note: if you insert "alert(test)" it returns "second"
        document.getElementById('test').value = test;
    }
</script>
<script type="text/javascript">
    getText = function() {      
        var test = document.getElementById('test').value;
        test = "second";
        //note: if you insert "alert(test)" it returns "second"
        document.getElementById('test').value = test;
    }
</script>
眉目亦如画i 2025-01-25 15:33:53

您需要这样做:

document.getElementById('test').value = "second";

或者

var el = dcument.getElementById('test');
el.value = "second";

关于原因,我相信这与JavaScript是“通过参考”或“通过价值”语言有关/stackoverflow.com/questions/518000/is-javascript-is-a-a-pass-by-reference-orference-or-pass-by-value-language">非常有趣的讨论此处。 (我不确定这一点,如果我错了,请纠正我)。

You need to do this:

document.getElementById('test').value = "second";

or

var el = dcument.getElementById('test');
el.value = "second";

As for why, I believe it has to do with something about Javascript being a "pass by reference" or "pass by value" language, on which subject there was a very interesting discussion here on SO. (I'm not sure on this point, correct me if I'm wrong).

菩提树下叶撕阳。 2025-01-25 15:33:53

因为它是一个字符串,并且作为值传递,而不是作为引用传递。因此value的内容被复制到test

because it's a string and is passed as value, not as reference. so the content of value is copied to test

寂寞花火° 2025-01-25 15:33:53

因为您将 value test作为字符串document.getElementById('test')。值

您不是将两者链接在一起。

如果您想这样做,则可以使用一个函数:

function test(x) {
  document.getElementById('test').value = x;
}

test('foo');

在Python中,您可以执行此操作。在JavaScript中,我不这么认为。

Because you're setting the value of test to be the string document.getElementById('test').value.

You aren't linking the two together.

If you are looking to do that, you can use a function:

function test(x) {
  document.getElementById('test').value = x;
}

test('foo');

In Python, you can do this. In JavaScript, I don't think so.

凉风有信 2025-01-25 15:33:52

因为JavaScript分配了X作为值,而不是对原始对象的引用。

例如,您可以:您可以:

function setText(x) {
    document.getElementById('test').value = x;
}

getText = function() {      
    return document.getElementById('test').value;
}

使用setText()设置的值将由getText()反映,因为getText()将还使用参考对象的值,而不是值的副本。

编辑

正如布莱恩(Bryan)指出的那样,这将是具有全局范围的参考副本:

var test = document.getElementById('test');

function setText(x) {
    test.value = x;
}

getText = function() {      
    return test.value;
}

http:http:/ /jsfiddle.net/nlj2a/

原始test变量存储对元素的引用,而不是与元素属性相关的值。

Because Javascript assigned x as a value and not a reference to the original object.

For example, you could instead:

function setText(x) {
    document.getElementById('test').value = x;
}

getText = function() {      
    return document.getElementById('test').value;
}

And the value you set with setText() will be reflected by getText(), since getText() will also use the reference object's value, and not a copy of the value.

EDIT

As Bryan points out, this would be a copy by reference with a global scope:

var test = document.getElementById('test');

function setText(x) {
    test.value = x;
}

getText = function() {      
    return test.value;
}

http://jsfiddle.net/nLj2A/

The original test variable stores a reference to the element, not a value associated with an attribute of the element.

人疚 2025-01-25 15:33:52

您正在将值复制到变量。更改变量不会更改原始变量,因为变量只包含一个副本。

如果将元素的引用存储在变量中,则可以使用它来设置值:

var test = document.getElementById('test');
test.value = "second";

You are copying the value to a variable. Changing the variable won't change the original, because the variable just contains a copy.

If you store the reference of the element in the variable, you can use that to set the value:

var test = document.getElementById('test');
test.value = "second";
巷子口的你 2025-01-25 15:33:52

您将元素的值分配给变量,然后更改变量。这并不反映在元素的值中。您需要更改元素的值。

document.getElementById('test').value = "second";

You're assigning the element's value to a variable and then changing the variable. This is not reflected back in the element's value. You need to change the element's value instead.

document.getElementById('test').value = "second";
誰ツ都不明白 2025-01-25 15:33:52

因为

document.getElementById('test').value

是 getter,as

document.getElementById('test').value = "second"

是 setter

because

document.getElementById('test').value

is a getter where as

document.getElementById('test').value = "second"

is a setter

故乡的云 2025-01-25 15:33:52
test = document.getElementById('test').value;

...只为您提供该时刻的值的副本。当您修改 test 时,您需要将其放回到您想要更改的输入字段中:

var test_input = document.getElementById('test');
test_input.value = "second";
test = document.getElementById('test').value;

...only gives you a copy of the value at that instant. When you modify test, you need to put that back into input field you'd like to change:

var test_input = document.getElementById('test');
test_input.value = "second";
木緿 2025-01-25 15:33:52

将局部变量 test 设置为“second”不会执行任何操作。我假设您希望 getText 更新 DOM。试试这个:

 getText = function() { document.GetElementById('test').value("second"); }

Setting the local variable test to "second" will do nothing. I assume you want getText to update the DOM. Try this:

 getText = function() { document.GetElementById('test').value("second"); }
甜心小果奶 2025-01-25 15:33:52

指向元素而不是值: http://jsbin.com/axufi4/edit

Point to element instead of value: http://jsbin.com/axufi4/edit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文