Javascript 访问javascript中的隐藏字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13579995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:24:03  来源:igfitidea点击:

Accessing hidden field value in javascript

javascriptasp.netvb.nethidden-field

提问by user1835316

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,

我的表单中有一个隐藏字段,我在服务器上设置隐藏字段的值并尝试从 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.

如果我查看源,隐藏字段值被设置,隐藏字段的 ID 与我调用的 ID 相同。

ASPX

ASPX

            var v = document.getElementById('hxValue').value;
            <asp:HiddenField ID="hxValue" runat="server"/>

VB

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.

我记得以前这样做过,它应该相对简单,但由于某种原因,我没有做对。

回答by Akhil Sekharan

Your 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

您需要使用以下方法找到客户端 ID

'<%=hxValue.ClientID%>'

回答by user1835316

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.

感谢所有的帮助。

回答by Darren

You can use innerTextnot valueto retrieve the value of hxValue.

您可以使用innerTextnotvalue来检索 hxValue 的值。

var v = document.getElementById('hxValue').innerText

If you were using jQueryyou could also do

如果你正在使用jQuery你也可以做

var v = $("#hxValue").val();

回答by polin

Try <asp:HiddenField ID="hxValue" runat="server" Value=""/>
Then call it by id and set value

尝试<asp:HiddenField ID="hxValue" runat="server" Value=""/>
然后通过 id 调用它并设置值

回答by yogi

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.

问题是隐藏字段是服务器端控件,您提供的 ID 是服务器端 ID,您必须获取该控件的客户端 ID 才能在客户端 JavaScript 或 Jquery 中引用它。

Update

更新

put this script at the end of your page, just before </body>something like this

将此脚本放在页面的末尾,就在</body>类似这样的内容之前

<script type="text/javascript" language="javascript">
  var v = document.getElementById('<%= hxValue.ClientID %>').value;
</script>
</body>