我在网页上有这个标记
<input type="hidden" name="ctl00_ed3327f9_e1db_40db_9d66_15946cead7ae_STATEFIELD" id="ctl00_0g_ed3327f9_e1db_40db_9d66_15946cead7ae__STATEFIELD" value="0" />
我想使用JavaScript修改其值,即从value="0"
到value="1"
。我试过了:
document.getElementById("ctl00_0g_ed3327f9_e1db_40db_9d66_15946cead7ae__STATEFIELD");
但它会返回null
。我猜那是因为它是一个隐藏的领域
答案 0 :(得分:1)
如果getElementById
返回null
,可能是因为您在创建元素之前运行了脚本。
将您的脚本移至页面底部,就在结束</body>
标记内:
<body>
<!-- page content -->
<script type="text/javascript">
document.getElementById("ctl00_0g_ed3327f9_e1db_40db_9d66_15946cead7ae__STATEFIELD").value = 1;
</script>
</body>
或在window.onload
处理程序内运行:
<script type="text/javascript">
window.onload = function() {
document.getElementById("ctl00_0g_ed3327f9_e1db_40db_9d66_15946cead7ae__STATEFIELD").value = 1;
};
</script>
答案 1 :(得分:0)
对于.NET 3.5及更早版本,请使用
document.getElementById('<%= controlName.ClientID %>');
对于.NET 4.0,将控件上的ClientIDMode
设置为Static
。
答案 2 :(得分:0)
您已添加jquery标记,因此我认为您正在使用它。这将是您的解决方案:
$("#ctl00_0g_ed3327f9_e1db_40db_9d66_15946cead7ae__STATEFIELD").val("1");
答案 3 :(得分:0)
我刚刚测试了你的代码并且它有效。隐藏不应该有所作为。我猜测问题可能是你在元素加载到页面之前尝试运行getElementById?