Javascript 使用 JQuery 设置隐藏字段的值

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

Set the Value of a Hidden field using JQuery

javascriptjquerysethidden-field

提问by JZ.

I want to set the value of a hidden field, using JQuery.

我想使用 JQuery 设置隐藏字段的值。

Hidden Field:

隐藏字段:

<input id="chag_sort" type="hidden" name="chag_sort">

My JQuery:

我的jQuery:

 $("#input[name=chag_sort]").val(sort2);

What am I doing wrong? I should also mention in console that sort2 does in fact have a value: DESC.

我究竟做错了什么?我还应该在控制台中提到 sort2 实际上有一个值:DESC。

回答by Darin Dimitrov

The selector should not be #input. That means a field with id="input"which is not your case. You want:

选择器不应该是#input. 这意味着一个领域id="input"不是你的情况。你要:

$('#chag_sort').val(sort2);

Or if your hidden input didn't have an unique id but only a name="chag_sort":

或者,如果您的隐藏输入没有唯一的 id 而只有一个name="chag_sort"

$('input[name="chag_sort"]').val(sort2);

回答by planetjones

Drop the hash - that's for identifying the id attribute.

删除哈希 - 用于识别 id 属性。

回答by Sajidur Rahman

If you have a hidden field like this

如果你有这样的隐藏字段

  <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("VertragNr") %>'/>

Now you can use your value like this

现在你可以像这样使用你的价值

$(this).parent().find('input[type=hidden]').val()

$(this).parent().find('input[type=hidden]').val()