【发布时间】:2013-09-27 16:36:42
【问题描述】:
我使用this SO answer 作为向我的服务器端发送 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>
【问题讨论】:
-
你不能在代码隐藏文件中使用
inAction.SelectedValue吗? -
出于某种原因,对于 IE 7 8 和 9,inAction.SelectedValue 总是为 null。这是我尝试一个笨拙的解决方案的根本原因。
-
尝试 Request.Form["clientSelection"] 代替。
-
尝试使用
UniqueID而不是ClientID。 -
试过 Request.Form["clientSelection"] 和 UniqueID,都不起作用。
标签: javascript asp.net webforms