使用 jquery 更改隐藏字段值并在服务器中获取新值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9639152/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:05:28  来源:igfitidea点击:

Change hidden field Value with jquery and get the new value in server

jqueryasp.nethtml

提问by Mahdi jokar

I change value off the hidden field with jquery and now i want to get the new value in server. i use asp.net and this is my jquery code:

我使用 jquery 更改隐藏字段的值,现在我想在服务器中获取新值。我使用 asp.net,这是我的 jquery 代码:

$('#HiddenField').val("NewValue");

and this is my html tag:

这是我的 html 标签:

<input id="HiddenField" type="hidden" runat="server" value=""/>

in my page i change the value of the '#HiddenField'and i want to get the NewValue in server side.

在我的页面中,我更改了 的值,'#HiddenField'我想在服务器端获取 NewValue。

回答by Akhil

You need to use the ClientID of your hidden field in the JQuery selector such as:

您需要在 JQuery 选择器中使用隐藏字段的 ClientID,例如:

$('#<%= HiddenField.ClientID %>').val("NewValue");

Or, alternatively, use a style for the hidden field and access it by the class, such as:

或者,或者,为隐藏字段使用样式并通过类访问它,例如:

<input id="HiddenField" type="hidden" runat="server" value="" CssClass="hidden"/>
$('.hidden').val("NewValue");

回答by labroo

I tried with this on my page,

我在我的页面上尝试过,

<!DOCTYPE html>
<html>
<head runat="server">
    <title></title>
</head>
<body>

    <form id="form1" runat="server">
    <div>
        <asp:Button runat="server" ID="goBtn" Text="Go" OnClick="goBtn_Click" />
        <input id="HiddenField" type="hidden" runat="server" value="" />
        <asp:TextBox runat="server" ID="testTxt"></asp:TextBox>
    </div>
    </form>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#<%=HiddenField.ClientID %>').val("Test");
        });
    </script>

</body>
</html>

and this in the code behind,

这在后面的代码中,

protected void goBtn_Click(object sender, EventArgs e)
{
    testTxt.Text = HiddenField.Value;
}

when you press the go button, the new value is available on the server.

当您按下 go 按钮时,新值在服务器上可用。