Javascript 如何在javascript中获取隐藏元素值

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

how to get hidden element value in javascript

javascript

提问by GuruKulki

how to get hidden element value in javascript

如何在javascript中获取隐藏元素值

回答by user241924

using the following document function

使用以下文档功能

document.getElementById("elementId").value;

elementId-> id defined for the hidden element

elementId-> 为隐藏元素定义的 id

回答by rahul

If the element has an id attribute and if it has a value attribute then you can use value property

如果元素有一个 id 属性并且它有一个 value 属性,那么你可以使用 value 属性

document.getElementById( "hidElem" ).value;

for a hidden input element like

对于隐藏的输入元素,如

<input type="hidden" id="hidElem" />

otherwise you can use textContentproperty

否则你可以使用textContent财产

document.getElementById( "hidElem" ).textContent;

for a hidden div element like

对于隐藏的 div 元素,例如

<div style="display: none;" id="hidElem">value of hidden element</div>

回答by nickf

Putting ids on all your elements is overkill in my opinion. You can access them by their name attribute - all you need is a reference to the form object.

在我看来,将 id 放在所有元素上是多余的。您可以通过它们的 name 属性访问它们 - 您需要的只是对表单对象的引用。

<form id="blah">
    <input type="hidden" name="inputOne" value="1" />
    <input type="hidden" name="inputTwo" value="2" />
</form>


var formObj = document.getElementById('blah');
alert("Input one value: " + formObj.inputOne.value);
alert("Input two value: " + formObj.inputTwo.value);

This applies to all types of form input.

这适用于所有类型的表单输入。