Javascript jQuery访问输入隐藏值

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

jQuery access input hidden value

javascriptjquery

提问by I-M-JM

How can I access <input type="hidden">tag's valueattribute using jQuery?

如何使用 jQuery访问<input type="hidden">标签的value属性?

回答by Tatu Ulmanen

You can access hidden fields' values with val(), just like you can do on any other input element:

您可以使用 访问隐藏字段的值val(),就像您可以在任何其他输入元素上执行的操作一样:

<input type="hidden" id="foo" name="zyx" value="bar" />

alert($('input#foo').val());
alert($('input[name=zyx]').val());
alert($('input[type=hidden]').val());
alert($(':hidden#foo').val());
alert($('input:hidden[name=zyx]').val());

Those all mean the same thing in this example.

在这个例子中,这些都意味着同样的事情。

回答by kaiser

There's a jQuery selector for that:

有一个 jQuery 选择器:

// Get all form fields that are hidden
var hidden_fields = $( this ).find( 'input:hidden' );

// Filter those which have a specific type
hidden_fields.attr( 'text' );

Will give you all hidden input fields and filter by those with a specific type="".

将为您提供所有隐藏的输入字段,并按具有特定type="".

回答by the_root

To get value, use:

要获得价值,请使用:

$.each($('input'),function(i,val){
    if($(this).attr("type")=="hidden"){
        var valueOfHidFiled=$(this).val();
        alert(valueOfHidFiled);
    }
});

or:

或者:

var valueOfHidFiled=$('input[type=hidden]').val();
alert(valueOfHidFiled);

To set value, use:

要设置值,请使用:

$('input[type=hidden]').attr('value',newValue);

回答by Maxim Sloyko

There is nothing special about <input type="hidden">:

没有什么特别之处<input type="hidden">

$('input[type="hidden"]').val()

回答by Rodrigo Longo

If you want to select an individual hidden field, you can select it through the different selectors of jQuery :

如果要选择单个隐藏字段,可以通过 jQuery 的不同选择器选择它:

<input type="hidden" id="hiddenField" name="hiddenField" class="hiddenField"/> 


$("#hiddenField").val(); //by id
$("[name='hiddenField']").val(); // by name
$(".hiddenField").val(); // by class

回答by Bruno Ferreira

If you have an asp.net HiddenField you need to:

如果你有一个 asp.net HiddenField 你需要:

To access HiddenField Value:

要访问 HiddenField 值:

$('#<%=HF.ClientID%>').val()  // HF = your hiddenfield ID

To set HiddenFieldValue

设置 HiddenFieldValue

$('#<%=HF.ClientID%>').val('some value')   // HF = your hiddenfield ID

回答by Georg Patscheider

Watch out if you want to retrieve a boolean value from a hidden field!

如果您想从隐藏字段中检索布尔值,请注意!

For example:

例如:

<input type="hidden" id="SomeBoolean" value="False"/>

(An input like this will be rendered by ASP MVC if you use @Html.HiddenFor(m => m.SomeBoolean).)

(如果您使用 ,这样的输入将由 ASP MVC 呈现@Html.HiddenFor(m => m.SomeBoolean)。)

Then the following will return a string 'False', not a JS boolean!

然后下面将返回一个 string 'False',而不是一个 JS 布尔值!

var notABool = $('#SomeBoolean').val();

If you want to use the boolean for some logic, use the following instead:

如果要对某些逻辑使用布尔值,请改用以下代码:

var aBool = $('#SomeBoolean').val() === 'True';
if (aBool) { /* ...*/ }

回答by Marcin ?urek

Most universal way is to take value by name. It doesn't matter if its input or select form element type.

最普遍的方法是按名称取值。它的输入或选择表单元素类型无关紧要。

var value = $('[name="foo"]');