admin管理员组文章数量:1220485
I have a hidden field in my form, I'm setting the hidden field's value on the server and trying to access this value from javascript,
I get the error: Unable to get value of the property 'value': object is null or undefined
If I view the source the hidden field value is set and the ID of the hidden field is the same as the ID I'm calling.
ASPX
var v = document.getElementById('hxValue').value;
<asp:HiddenField ID="hxValue" runat="server"/>
VB
hxValue.Value = "Value1"
I recall doing this before and it should be relatively simple but for some reason, i'm not getting it right.
I have a hidden field in my form, I'm setting the hidden field's value on the server and trying to access this value from javascript,
I get the error: Unable to get value of the property 'value': object is null or undefined
If I view the source the hidden field value is set and the ID of the hidden field is the same as the ID I'm calling.
ASPX
var v = document.getElementById('hxValue').value;
<asp:HiddenField ID="hxValue" runat="server"/>
VB
hxValue.Value = "Value1"
I recall doing this before and it should be relatively simple but for some reason, i'm not getting it right.
Share Improve this question asked Nov 27, 2012 at 8:22 user1835316user1835316 1271 gold badge2 silver badges9 bronze badges5 Answers
Reset to default 5Your code will work. For simple forms, just add
<asp:HiddenField ClientIDMode="static" ID="hxValue" runat="server"/>
OR
you need to find the client id using
'<%=hxValue.ClientID%>'
Ok it appears my hidden field's value had not been set before the script had run, therefore receiving a null value. I had assumed that placing breakpoints on the server page load and the script would establish if the control was being set before the script ran, appears not.
Fixed as below:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script type="text/javascript">
function GetHiddenValues() {
var v = document.getElementById('<%= hxValue.ClientID %>').value;
}
</script>
</head>
<body onload="GetHiddenValues() ;">
<form runat="server">
<asp:HiddenField ClientIDMode="static" ID="hxValue" runat="server"/>
</form>
</body>
</html>
Thanks for all the assistance.
You can use innerText
not value
to retrieve the value of hxValue.
var v = document.getElementById('hxValue').innerText
If you were using jQuery
you could also do
var v = $("#hxValue").val();
Try this
var v = document.getElementById('<%= hxValue.ClientID %>').value;
Problem is that Hidden Field is server side control and it's ID that you have given is a server side ID, you will have to get client side ID of that control to refer it in your client side JavaScript or Jquery.
Update
put this script at the end of your page, just before </body>
something like this
<script type="text/javascript" language="javascript">
var v = document.getElementById('<%= hxValue.ClientID %>').value;
</script>
</body>
Try <asp:HiddenField ID="hxValue" runat="server" Value=""/>
Then call it by id and set value
本文标签: aspnetAccessing hidden field value in javascriptStack Overflow
版权声明:本文标题:asp.net - Accessing hidden field value in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739319709a2157976.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论