如何将变量从 jquery 传递到代码隐藏(C#)?
这是我的代码,但它不起作用(后面的代码获取空字符串): `
<head id="Head1" runat="server">
<title>Pass Javascript Variables to Server</title>
<script type="text/javascript"> // Second Step
function f() {
$("[id$='inpHide']").val("My JavaScript Value");
}
</script>
</head>
<body onload="SetHiddenVariable()">
<form id="form1" runat="server">
<div>
<input id="inpHide" type="hidden" runat="server" />
<asp:Button ID="btnJSValue" Text="Click" runat="server" OnClientClick="f"/>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
`
Here is my code, but it doesn't work (code behind gets empty string):
`
<head id="Head1" runat="server">
<title>Pass Javascript Variables to Server</title>
<script type="text/javascript"> // Second Step
function f() {
$("[id$='inpHide']").val("My JavaScript Value");
}
</script>
</head>
<body onload="SetHiddenVariable()">
<form id="form1" runat="server">
<div>
<input id="inpHide" type="hidden" runat="server" />
<asp:Button ID="btnJSValue" Text="Click" runat="server" OnClientClick="f"/>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码中有几个问题。在
onClientClick
中调用f
函数时缺少括号,如果元素有 id,您只需使用 id 通过#
选择它There were couple of issues in your code. The parenthesis were missing while calling
f
function inonClientClick
and if the element has id you can just use the id to select it with#
您的
标记需要 name 属性,否则不会发布。
Your
<input>
tag needs a name attribute, or it won't be posted.肖恩·麦克米兰的回应是正确的,但我认为你还需要改变这一点
:
The response from Sean McMillan is right, but I think you also need to change this :
to