0

Can someone tell me why this markup/script errors out and doesnt put "Waltdog" into the Hidden1 input field?

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<script>
    document.getElementById("Hidden1").value = 'Waltdog';
</script>
<body>
    <form id="form1" runat="server">
        <div>
            <input id="Hidden1" type="hidden" runat="server" /> 
            Let me know if you see this!!! You crazy Texicans are harshing my Wednesday vibe! :)
        </div>
    </form>
</body>

4

2 回答 2

3

因为您的脚本在元素存在之前运行。

HTML(连同脚本标签中的 javascript)被从上到下解释,因此当脚本运行时,输入元素尚未创建。

解决方案是

  • 将脚本放在页面加载时运行的函数中
  • 将javascript放在元素之后
于 2012-05-16T15:59:54.593 回答
0

将脚本节点放在输入下方

<input id="Hidden1" type="hidden" runat="server" /> 
<script>
    document.getElementById("Hidden1").value = 'Waltdog';
</script>
于 2012-05-16T17:01:17.173 回答