如果我设置`le Let x = document.getElementById('inputText" quot; quot; quot value'并更新'x`,为什么do nor do doal noce Update not doal the Value Update?
在下面的示例中,为什么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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
尽管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)
您需要这样做:
或者
关于原因,我相信这与JavaScript是“通过参考”或“通过价值”语言有关/stackoverflow.com/questions/518000/is-javascript-is-a-a-pass-by-reference-orference-or-pass-by-value-language">非常有趣的讨论此处。 (我不确定这一点,如果我错了,请纠正我)。
You need to do this:
or
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).
因为它是一个字符串,并且作为值传递,而不是作为引用传递。因此value的内容被复制到test
because it's a string and is passed as value, not as reference. so the content of value is copied to test
因为您将 value
test
作为字符串document.getElementById('test')。值
。您不是将两者链接在一起。
如果您想这样做,则可以使用一个函数:
在Python中,您可以执行此操作。在JavaScript中,我不这么认为。
Because you're setting the value of
test
to be the stringdocument.getElementById('test').value
.You aren't linking the two together.
If you are looking to do that, you can use a function:
In Python, you can do this. In JavaScript, I don't think so.
因为JavaScript分配了X作为值,而不是对原始对象的引用。
例如,您可以:您可以:
使用
setText()
设置的值将由getText()
反映,因为getText()
将还使用参考对象的值,而不是值的副本。编辑
正如布莱恩(Bryan)指出的那样,这将是具有全局范围的参考副本:
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:
And the value you set with
setText()
will be reflected bygetText()
, sincegetText()
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:
http://jsfiddle.net/nLj2A/
The original
test
variable stores a reference to the element, not a value associated with an attribute of the element.您正在将值复制到变量。更改变量不会更改原始变量,因为变量只包含一个副本。
如果将元素的引用存储在变量中,则可以使用它来设置值:
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:
您将元素的值分配给变量,然后更改变量。这并不反映在元素的值中。您需要更改元素的值。
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.
因为
是 getter,as
是 setter
because
is a getter where as
is a setter
...只为您提供该时刻的值的副本。当您修改
test
时,您需要将其放回到您想要更改的输入字段中:...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:将局部变量
test
设置为“second”不会执行任何操作。我假设您希望 getText 更新 DOM。试试这个:Setting the local variable
test
to "second" will do nothing. I assume you want getText to update the DOM. Try this:指向元素而不是值: http://jsbin.com/axufi4/edit
Point to element instead of value: http://jsbin.com/axufi4/edit