如何从 jQuery/JavaScript 为 Html.Hiddenfor 赋值?

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

How can I assign a value to a Html.Hiddenfor from jQuery/ JavaScript?

javascriptjqueryasp.netasp.net-mvc

提问by SRA

I have a hidden asp.net MVC control in the form:

我有一个隐藏的 asp.net MVC 控件的形式:

<%= Html.HiddenFor(m => m.NodeId) %>

My JavaScript / jQuery code:

我的 JavaScript/jQuery 代码:

var DeleteEntireItem = '<% = btnDeleteEntireMenu.ClientID%>';
var Node;
debugger;

$('#' + DeleteEntireItem).click(function () {
    Node = NodeValue;
    document.forms[0].submit();
});

How can I assign the value of variable 'Node' to the asp.net MVC hidden control?

如何将变量“节点”的值分配给 asp.net MVC 隐藏控件?

回答by Stefaan

You can set the value attribute of a hidden input tag by selecting it using the property name and using the jquery val() method.

您可以通过使用属性名称和使用 jquery val() 方法来选择隐藏输入标签的 value 属性。

In your case this becomes:

在你的情况下,这变成:

$("#NodeId").val(Node)