javascript 如何使用javascript访问隐藏的输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16825402/
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
How to access a hidden input with javascript
提问by Austin Lovell
What I am attempting to do is is access hidden object in a div. What happends is a user will click on button that will then perform some task such as delete a user. This may be easier if I show what I have.
我试图做的是访问 div 中的隐藏对象。发生的事情是用户将单击按钮,然后执行某些任务,例如删除用户。如果我展示我所拥有的,这可能会更容易。
<div class="mgLine">
<input type="hidden" name="tenentID" value="1">
<p class="mgName">James Gear</p>
<input type="text" class="mgBill" value="" placeholder="Post Bill Link Here">
<a href="#" class="mgButton" onclick="alertTest(this)">Submit Bill</a>
<a href="#" class="mgNP">Not Paid</a>
<a href="#" class="mgButton">Change Password</a>
<a href="#" class="mgButton">Delete User</a>
</div>
What I want the system to do is alert the value of one which it gets from the hidden field when the "submit bill" is pressed.
我希望系统做的是在按下“提交账单”时提醒它从隐藏字段中获取的值。
function alertTest(e){
//e.parentNode
window.alert(e.parentNode.getElementsByTagName("tenentID")[0]);
}
I am attempting to use JavaScript DOM to access the element. I hope this made at least some sense. There will be many of these entries on the page.
我正在尝试使用 JavaScript DOM 来访问元素。我希望这至少有意义。页面上会有很多这样的条目。
回答by PSL
You need to use getElementsByName
instead of getElementsByTagName
你需要使用getElementsByName
而不是getElementsByTagName
function alertTest(e){
//e.parentNode
window.alert(document.getElementsByName("tenentID")[0]);
}
getElementsByTagName is for selecting elements based on its tag say div, input etc..
getElementsByTagName 用于根据其标签选择元素,例如 div、input 等。
getElementsByName
按名称获取元素
getElementsByTagName
getElementsByTagName
Realizing that you might have multiple div section and your hidden input is the first child you could use these:-
意识到您可能有多个 div 部分并且您的隐藏输入是您可以使用这些的第一个孩子:-
e.parentNode.getElementsByTagName("input")[0].value;
or
或者
e.parentNode.firstElementChild.value;
if it is not the firstCHild and you know the position then you could use
如果它不是第一个孩子并且您知道位置,那么您可以使用
e.parentNode.children(n).value; //n is zero indexed here
Fiddle
小提琴
回答by Explosion Pills
The modern method would be to use querySelector
.
现代方法是使用querySelector
.
e.parentNode.querySelector("[name=tenentID]");
http://jsfiddle.net/ExplosionPIlls/zU2Gh/
http://jsfiddle.net/ExplosionPIlls/zU2Gh/
However you could also do it with some more manual DOM parsing:
但是,您也可以通过一些更手动的 DOM 解析来实现:
var nodes = e.parentNode.getElementsByTagName("input"), x;
for (x = 0; x < nodes.length; x++) {
if (nodes[x].name === "tenentID") {
console.log(nodes[x]);
}
}
回答by Yotam Omer
Try this:
试试这个:
function alertTest(e){
alert(e.parentNode.getElementsByName("tenentID")[0].value);
}
回答by Les Ferguson
I usually set an id attribute on the hidden element, then use getElementById
.
我通常在隐藏元素上设置一个 id 属性,然后使用getElementById
.
<input type="hidden" id="tenentID" name="tenentID" value="1">
then I can use...
那我可以用...
var tenentValue = document.getElementById("tenentID").value;
回答by MBielski
In general, your best bet for accessing a specific element is to give it an ID and then use getElementById().
通常,访问特定元素的最佳选择是为其提供 ID,然后使用 getElementById()。
function alertTest(ID){
alert(document.getElementById(ID).value);
}
Names can be duplicated on a page, but the ids have to be unique.
名称可以在页面上重复,但 ID 必须是唯一的。