【发布时间】:2019-07-17 21:15:57
【问题描述】:
我在表单中使用 ASP.NEt hiddenfield:
<form id="form1" runat="server">
<asp:HiddenField ID="ScreenWidth" runat="server" />
<asp:HiddenField ID="ScreenHeight" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</form>
这是JS代码:
<script type="text/javascript">
$(document).ready(function() {
alert(document.getElementById("ScreenWidth").val);
document.getElementById("ScreenWidth").val = screen.width;
document.getElementById("ScreenHeight").val = screen.height;
alert(document.getElementById("ScreenWidth").val);
});
但是当我访问 onsubmit 函数上的值时 - 它们是空的。 警报显示未定义,然后在触发时显示一个值 - 所以 JS.部分工作......
如何获取 c# 代码中的值?
myheight = ScreenHeight.Value.ToString();
mywidth = ScreenWidth.Value.ToString();
【问题讨论】:
-
尝试添加 ClientIDMode="Static" 和 enableViewState = true 但没有帮助。
-
尝试使用完整的单词
value,而不是原版js的val。
标签: asp.net hidden-field