Javascript Variables

Javascript Variables

本文关键字:Variables Javascript      更新时间:2023-09-26

为什么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>

会变的

因为您不更改input对象的值,只更改变量。

元素独立于变量。如果你想改变浏览器显示的内容,你必须这样做:

document.getElementById("...").value = window.surveyName;

因为您还没有点击输入

是的。但是您已经将变量输出到页面,并且您输出的不会改变。

修改input的值:

document.getElementById("survey").value = "new value";

这是有效的,因为您已经为元素分配了id"survey",所以您可以通过document.getElementById传入该id来检索元素,并使用元素的value属性来设置该值。