简体   繁体   English

如何获取具有不同id的两个隐藏标签的值

[英]how to fetch values of two hidden tags with different id

I have using two anchor tag to transfer the control to same page. 我使用两个锚标签将控件转移到同一页面。 and gives two hidden input with same id but different values. 并给出两个具有相同id但不同值的隐藏输入。 as shown in given code. 如给定的代码所示。

 <li data-icon="false"><a href="#paymentReceiptVoucher" onclick="loadAccForPayVoucher();">
        <input type="hidden" id="PRVou" value="payment">PaymentReceipt Voucher</a></li>

<li data-icon="false"><a href="#paymentReceiptVoucher" onclick="loadAccForPayVoucher();">
        <input type="hidden" id="PRVou" value="receipt">ReceiptPayment Voucher</a></li>

and i want to get the values of these hidden tags by using following javascript code. 我想通过使用以下javascript代码获取这些隐藏标签的值。

loadAccForPayVoucher = function() {
    alert(document.getElementById('PRVou').value);
}

and it always alert payment . 它始终提醒payment how can i get the value according to the link. 我怎样才能根据链接获得价值。 Thanks. 谢谢。

id attributes are meant to be unique, regardless of their context. 无论其上下文如何, id属性都是唯一的。 There should only be one element in the entire document with a given id . 整个文档中只应该有一个具有给定id元素。

Give the element a class name instead: 给元素一个类名:

<input type="hidden" class="PRVou" value="payment">

And then use getElementsByClassName : 然后使用getElementsByClassName

document.getElementsByClassName('PRVou')[0].value

Try this: 尝试这个:

<li data-icon="false"> <a href="#paymentReceiptVoucher" onclick="loadAccForPayVoucher('payment');">
        <!--<input type="hidden" id="PRVou" value="payment">PaymentReceipt Voucher</a> --></li>

<li data-icon="false"><a href="#paymentReceiptVoucher" onclick="loadAccForPayVoucher('receipt');">
        <!-- <input type="hidden" id="PRVou" value="receipt">ReceiptPayment Voucher</a> --></li>
<script type="text/javascript">
    loadAccForPayVoucher = function(type) {
        alert(type);
        //alert(document.getElementById('PRVou').value);
    }
</script>

In HTML the ID attributes are always unique, thus when you call document.getElementById , the DOM of the browser will go out and fetch any (most likely the first) element with the given ID. 在HTML中,ID属性始终是唯一的,因此当您调用document.getElementById ,浏览器的DOM将出去并获取具有给定ID的任何(很可能是第一个)元素。

What can I do? 我能做什么?

Give them separate IDs, your html will look like this: 给他们单独的ID,你的HTML将如下所示:

<li data-icon="false"><a href="#paymentReceiptVoucher" onclick="loadAccForPayVoucherPayment();">
     <input type="hidden" id="PRVou-payment" value="payment"/>PaymentReceipt Voucher</a></li>

<li data-icon="false"><a href="#paymentReceiptVoucher" onclick="loadAccForPayVoucherReceipt();">
    <input type="hidden" id="PRVou-receipt" value="receipt"/>ReceiptPayment Voucher</a></li>

And then your JavaScript will have separate event handlers: 然后你的JavaScript将有单独的事件处理程序:

loadAccForPayVoucherPayment = function() {
    alert(document.getElementById('PRVou-payment').value);
}
loadAccForPayVoucherReceipt = function() {
    alert(document.getElementById('PRVou-receipt').value);
}

Update : I made you a fiddle :) http://jsfiddle.net/YCXC8/ 更新 :我让你成为小提琴:) http://jsfiddle.net/YCXC8/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM