JavaScript > 如果我设置“var x = document.getElementById('inputText').value”并更新“x”,为什么值不更新?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6257984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:58:45  来源:igfitidea点击:

JavaScript > If I set "var x = document.getElementById('inputText').value" and I update "x", why doesn't the value update?

javascript

提问by sapiensgladio

In the following example, why doesn't the "value" of the input "test" update to "second"?

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

<html>
<head>
<script type="text/javascript">
    getText = function() {      
        var test = document.getElementById('test').value;
        test = "second";
       //note: if you insert "alert(test)" it returns "second"

    }
</script>
</head>
<body>
<label for="test">Test </label><input type="text" id="test" value="first" onclick="getText()"></input>
</body>
</html>

回答by Jared Farrish

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

因为 Javascript 将 x 分配为一个值而不是对原始对象的引用。

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.

并且您设置的值setText()将由 反映getText(),因为getText()还将使用引用对象的值,而不是该值的副本。

EDIT

编辑

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

正如 Bryan 指出的,这将是一个具有全局范围的引用副本:

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

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

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

http://jsfiddle.net/nLj2A/

http://jsfiddle.net/nLj2A/

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

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

回答by Guffa

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";

回答by BalusC

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";

回答by Blender

Because you're setting the valueof testto be the string document.getElementById('test').value.

因为你设置test为字符串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.

在 Python 中,你可以做到这一点。在 JavaScript 中,我不这么认为。

回答by locrizak

because

因为

document.getElementById('test').value

is a getter where as

是一个吸气剂,作为

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

is a setter

是一个二传手

回答by kafuchau

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:

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

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

回答by Richard Schneider

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

将局部变量设置test为“second”将什么也不做。我假设您希望 getText 更新 DOM。尝试这个:

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

回答by Drew

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

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

回答by tomfumb

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.

尽管 javascript 最终将一切都视为对象,但我相信只有数组和对象是通过引用传递的。字符串、整数和浮点数按值传递。

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

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

回答by mrtsherman

<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>