Asp .net 隐藏字段无法使用 jquery 设置值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17319384/
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
Asp .net hidden field can't set value with jquery
提问by Alecu
I can't set the value of a hidden field with jquery in asp .net.
我无法在 asp .net 中使用 jquery 设置隐藏字段的值。
My hidden field is declared like this:
我的隐藏字段声明如下:
<asp:HiddenField runat="server" ID="hdnSelectedTicket" />
And this is how I set the value:
这就是我设置值的方式:
alert(ticketID);
$('#<%=hdnSelectedTicket.ClientID %>').val(ticketID);
alert($('#<%=hdnSelectedTicket.ClientID %>').val());
Both alerts show the right value but when I fetch it at the server it is empty.
两个警报都显示正确的值,但是当我在服务器上获取它时它是空的。
采纳答案by Alecu
It turns out that I was putting the hidden field inside a div that was used as a model for jquery dialog. When I removed the hidden field from the div and place it somewhere else it worked.
事实证明,我将隐藏字段放在用作 jquery 对话框模型的 div 中。当我从 div 中删除隐藏字段并将其放在其他地方时,它起作用了。
回答by Shahid Iqbal
Set ClientIDMode="Static"
and then you can use $('#hdnSelectedTicket').val(ticketID);
to set the value in asp hidden field
设置ClientIDMode="Static"
然后就可以$('#hdnSelectedTicket').val(ticketID);
用来设置asp隐藏字段的值了
like
喜欢
asp:HiddenField ID="hdnSelectedTicket" runat="server" ClientIDMode="Static"
and
和
$('#hdnSelectedTicket').val(ticketID);
回答by Martin Ernst
Depending when you are reading the value on the server side, it might not be updated on the control yet - essentially if you are doing it in a change event handler, and the control that raises the change event gets updated before the hidden control, then calling hdnSelectedTicket.Value can still return the old value.
根据您在服务器端读取值的时间,它可能尚未在控件上更新 - 基本上如果您在更改事件处理程序中执行此操作,并且引发更改事件的控件在隐藏控件之前更新,然后调用 hdnSelectedTicket.Value 仍然可以返回旧值。
The easiest way to get around this issue is to cheat and get it straight from the Form collection:
解决此问题的最简单方法是作弊并直接从 Form 集合中获取它:
var ticketId = Request.Form[hdnSelectedTicket.UniqueID];