JavaScript 变量
为什么 SurveyName 的值没有改变?
<script type = 'text/javascript'>
surveyName = "catNull";
function test(){
window['surveyName'] = "catTest";
}
</script>
</head>
<body>
<input onclick = 'test()' id = 'cat' class = 'test' type = 'button' value = 'category' />
<script>
document.write('<input id = "survey" class = "test" type = "button" value = "'+surveyName+'" />');
</script>
Why doesn't surveyName's value change?
<script type = 'text/javascript'>
surveyName = "catNull";
function test(){
window['surveyName'] = "catTest";
}
</script>
</head>
<body>
<input onclick = 'test()' id = 'cat' class = 'test' type = 'button' value = 'category' />
<script>
document.write('<input id = "survey" class = "test" type = "button" value = "'+surveyName+'" />');
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它确实改变了。
但是,您永远不会观察到变化。
It does change.
However, you never observe the change.
因为您不更改
input
对象的值,仅更改变量。该元素独立于变量。
如果您想更改浏览器显示的内容,您必须执行以下操作:
Because you don't change
input
object's value, only the variable.The element is independent of the variable.
If you want to change what the browser shows, you must do:
因为你还没有点击输入。
Because you did not click on the input yet.
确实如此。但是您已经将变量输出到页面,并且您输出的内容不会改变。
如果您想更改
input
的值:这是可行的,因为您已为元素分配了
id
值“survey”
,因此您可以通过document.getElementById 检索元素
传入该值id
,并使用元素的value
属性 设置值。It does. But you've already output the variable to the page, and what you've output doesn't change.
If you want to change the
input
's value:That works because you've assigned the element the
id
value"survey"
, so you can retrieve the element viadocument.getElementById
passing in thatid
, and use the element'svalue
property to set the value.