我使用这个 SO 答案作为将 JavaScript 变量发送到我的服务器端的参考。但是,当我实施该解决方案时, JS 中的所有内容都正确显示alert()
,但是当我点击服务器时,我的隐藏字段的值始终为空。
JavaScript 和 HTML:
<script>
function rblSelectionChange()
{
var selection = $('#inAction input:checked').val();
var stuff = $('#<%= clientSelection.ClientID %>').val(selection);
alert(stuff.val());
}
</script>
<asp:HiddenField ID="clientSelection" runat="server" />
<div class="row-fluid">
<asp:RadioButtonList runat="server" ID="inAction" ClientIDMode="Static">
<asp:ListItem onClick="rblSelectionChange();" Value="RuEp" Text="I remember my <b>username</b>. Please email me a new <b>password</b>." />
<asp:ListItem onClick="rblSelectionChange();" Value="ReEu" Text="I remember my <b>email</b>. Please email me my <b>username</b>." />
<asp:ListItem onClick="rblSelectionChange();" Value="ReEup" Text="I remember my <b>email</b>. Please email me my <b>username</b> and a new <b>password</b>." />
</asp:RadioButtonList>
</div>
在页面的提交事件中,我尝试获取该值,但它是空的:
protected void btnActionSelect_Click(object sender, EventArgs e)
{
string selection = clientSelection.Value;
...snip...
}
知道我缺少什么吗?
更新
我试图将我的隐藏字段更改为纯 html,而不是 asp: 控件。
<input type="hidden" id="clientSelection" name="clientSelection" value="" />
我修改了代码隐藏如下:
private string _selection = "";
protected void Page_Load(object sender, EventArgs e)
{
//_selection = clientSelection.Value.ToString();
if (IsPostBack)
_selection = Request.Form["clientSelection"];
}
对于值 Request.Form [“clientSelection”],我仍然一无所获。但重要的是它适用于 Chrome、FF 和 IE10。我试图让它工作的浏览器是 IE 7 8 和 9。我完全被难住了。
更新 2
根据请求,这是我在 IE10 中检查页面时的页面来源(浏览器和文档模式设置为 IE7)
<DIV id=ctl00_cphBodyWithForm_htmActionSelect class=row-fluid>
<P class=lead>Can't access your account? Please select from the following options: </P>
<SCRIPT>
function rblSelectionChange()
{
var selection = $('#inAction input:checked').val();
var stuff = $('#ctl00_cphBodyWithForm_clientSelection').val(selection);
alert(stuff.val());
}
</SCRIPT>
<INPUT id=ctl00_cphBodyWithForm_clientSelection type=hidden name=ctl00$cphBodyWithForm$clientSelection jQuery191034119593101524303="7">
<DIV class=row-fluid>
<TABLE id=inAction border=0>
<TBODY>
<TR>
<TD><INPUT onclick=rblSelectionChange(); id=inAction_0 type=radio value=RuEp name=ctl00$cphBodyWithForm$inAction jQuery191034119593101524303="8"><LABEL for=inAction_0>I remember my <B>username</B>. Please email me a new <B>password</B>.</LABEL></TD>
</TR>
...snip...
</TBODY>
</TABLE>
</DIV>
<DIV class=span12>
<A class="submitButton roundedBR" href="javascript:__doPostBack('ctl00$cphBodyWithForm$ctl00','')">Continue > </A>
</DIV>
</DIV>