如何使用javascript访问隐藏的输入 [英] How to access a hidden input with javascript
问题描述
我想做的是在div中访问隐藏的对象。发生的情况是用户将单击按钮,然后将执行某些任务,例如删除用户。
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 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]);
}
我正在尝试使用JavaScript DOM访问元素。我希望这至少是有道理的。页面上会有很多这样的条目。
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.
推荐答案
您需要使用 getElementsByName
而不是 getElementsByTagName
function alertTest(e){
//e.parentNode
window.alert(document.getElementsByName("tenentID")[0]);
}
getElementsByTagName用于根据其标签(例如div,input等)选择元素。
getElementsByTagName is for selecting elements based on its tag say div, input etc..
意识到您可能有多个div部分,而隐藏的输入是您可以使用以下内容的第一个孩子:-
Realizing that you might have multiple div section and your hidden input is the first child you could use these:-
e.parentNode.getElementsByTagName("input")[0].value;
或
e.parentNode.firstElementChild.value;
如果它不是第一个CHILD并且您知道头寸,则可以使用
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
这篇关于如何使用javascript访问隐藏的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!