使用 jQuery 获取 asp:HiddenField 的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8908340/
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
Get the Value of an asp:HiddenField using jQuery
提问by Melanie
I have two pages. From the first page, I open a modal with a querystring that holds that value of a client name. I then use this to set a hiddenfield on the modal that opened.
我有两页。从第一页开始,我打开一个带有查询字符串的模式,其中包含客户端名称的值。然后我用它在打开的模态上设置一个隐藏字段。
I need a TextBox on the new modal to display the value that has been sent through from the first screen.
我需要新模式上的 TextBox 来显示从第一个屏幕发送的值。
I've tried getting the value using:
我尝试使用以下方法获取值:
var hv = $('hidClientField').val();`
But this doesn't seem to work.
但这似乎不起作用。
This is my hidden field:
这是我的隐藏字段:
<asp:HiddenField ID="hidClientName" runat="server" />`
I set it in the code behind on the Page_Load like this:
我将它设置在 Page_Load 后面的代码中,如下所示:
hidClientName.Value = Request.QueryString["Client_Name"] ?? "";`
Any ideas will be much appreciated.
任何想法将不胜感激。
回答by Amar Palsapure
Try any of the following
尝试以下任何一项
If ASP.Net control and javascript both are on same page, then use
var hv = $("#"+ '<%= hidClientField.ClientID %>').val();
If you want to access the control from some JS file, then
// 'id$' will cause jQuery to search control whose ID ends with 'hidClientField' var hv = $('input[id$=hidClientField]').val();
You can use class name selector to achieve same. Check out thissimilar question.
如果 ASP.Net 控件和 javascript 都在同一页面上,则使用
var hv = $("#"+ '<%= hidClientField.ClientID %>').val();
如果你想从某个 JS 文件访问控件,那么
// 'id$' will cause jQuery to search control whose ID ends with 'hidClientField' var hv = $('input[id$=hidClientField]').val();
您可以使用类名选择器来实现相同的目的。看看这个类似的问题。
In asp.net, controls id is mangled. Because of this your code is not working.
在 asp.net 中,控件 id 被破坏。因此,您的代码无法正常工作。
Hope this works for you.
希望这对你有用。
回答by Didier Ghys
You forgot the #
in your selector to select by ID:
您忘记了#
在选择器中按 ID 选择:
var hv = $('#hidClientField').val();
Although asp.net generates ID based on the naming containers so you might end up with an ID like ctl1$hidClientField
. You can then use the "attribute ends with" selector:
尽管 asp.net 根据命名容器生成 ID,因此您最终可能会得到类似ctl1$hidClientField
. 然后,您可以使用“属性结束于”选择器:
var hv = $('input[id$=hidClientField]').val();
Check the documentation about jQuery selectors
查看有关jQuery 选择器的文档
回答by adatapost
Use ID selector.
使用 ID 选择器。
var hv = $('#hidClientName').val();
Or
或者
var hv = $('#<%=hidClientName.ClientID%>').val();
回答by Mujassir Nasir
Because jQuery knows nothing about asp:HiddenField
. It looks in the HTML structure where you have <input type="hidden" name="ctl00$cph_main$HiddenFieldServerDateTime" id="ctl00_cph_main_HiddenFieldServerDateTime" ...
. So there's no input with ID= HiddenFieldServerDateTime
. There are a few ways to overcome this:
因为 jQuery 对asp:HiddenField
. 它在 HTML 结构中查找您拥有<input type="hidden" name="ctl00$cph_main$HiddenFieldServerDateTime" id="ctl00_cph_main_HiddenFieldServerDateTime" ...
. 所以没有输入ID= HiddenFieldServerDateTime
。有几种方法可以克服这个问题:
Use a css selector:<asp:HiddenField ID="HiddenFieldServerDateTime" runat="server" CssClass="SomeStyle" />
with the following selector:
var serverDateTime = $(".SomeStyle").val();
CssClass
is not an available class on theHiddenField
class (and it doesn't have anAttributes
collection, so you can't add it manually).Use
ClientID
property:var serverDateTime = $("#<%= HiddenFieldServerDateTime.ClientID %>").val();
Wrap the hidden field in something you can select:
<div class="date-time-wrap"> <asp:HiddenField ID="..." runat="server" /> </div>
var serverDateTime = $('.date-time-wrap input[type=hidden]').val();
使用 css 选择器:<asp:HiddenField ID="HiddenFieldServerDateTime" runat="server" CssClass="SomeStyle" />
使用以下选择器:
var serverDateTime = $(".SomeStyle").val();
CssClass
不是该类上的可用类HiddenField
(并且它没有Attributes
集合,因此您无法手动添加它)。使用
ClientID
属性:var serverDateTime = $("#<%= HiddenFieldServerDateTime.ClientID %>").val();
将隐藏字段包裹在您可以选择的内容中:
<div class="date-time-wrap"> <asp:HiddenField ID="..." runat="server" /> </div>
var serverDateTime = $('.date-time-wrap input[type=hidden]').val();
回答by Mujassir Nasir
If you are using Asp.net controls, the server will mangle the control ids. It adds a bunch of extraneous control tree hierarchy information into the id. You need to reference what that acutal id is that's getting rendered, which is availble with the ClientID property on the control (hfUser.ClientID) or access your control in a different, more roundabout way, like find the controls parent, then search through its children to find your control.
如果您使用的是 Asp.net 控件,服务器将修改控件 ID。它将一堆无关的控制树层次结构信息添加到 id 中。您需要引用正在呈现的实际 ID,这可通过控件 (hfUser.ClientID) 上的 ClientID 属性使用,或者以不同的、更迂回的方式访问您的控件,例如找到控件父级,然后搜索其孩子找到你的控制。
If you don't have to use the asp.net HiddenField control, try just using a regular old html input.
如果您不必使用 asp.net HiddenField 控件,请尝试使用常规的旧 html 输入。
回答by Ashfaq Shaikh
you can do in this way
你可以这样做
var hval = $('#<%= hdMarkupPercentage.ClientID%>').val();
回答by AYK
Include ClientIDMode="Static" in your code.
在您的代码中包含 ClientIDMode="Static"。
var obj = $('#hidClientName').val();
<asp:HiddenField ID="hidClientName" ClientIDMode="Static" runat="server" />
var obj = $('#hidClientName').val();
<asp:HiddenField ID="hidClientName" ClientIDMode="Static" runat="server" />
Or
或者
var obj = $('#<%=hidClientName.ClientID%>').val();
<asp:HiddenField ID="hidClientName" runat="server" />
var obj = $('#<%=hidClientName.ClientID%>').val();
<asp:HiddenField ID="hidClientName" runat="server" />
回答by Glenn
Please try this code.
请试试这个代码。
var hv = $("#<%= hidClientField.ClientID %>").val();