简体   繁体   中英

Get hidden input value in td JavaScript

I have a table that is generated in the cycle. There are a fields, clicking on which should appear in the Alert with "hidden input value" of this particular cell. Id of all the same for all.Is it possible to specify a condition: INPUT id that is in a td, in which the press? Trying to make it through the "this." - but failed. Here my code: http://jsfiddle.net/nsy04nfr/

<td>
<a href=''  onclick='sendVar()'>    
        Check 192.168.0.0
        <input id='ip_id' type='hidden' value='192.168.0.0'>
</a>

    <script>
        function sendVar() {
            var ip= document.getElementById('ip_id').value;         
            alert(ip); 
        }
    </script>
    </td><br><br>
<td>
<a href=''  onclick='sendVar()'>    
        Check 255.255.255.255
        <input id='ip_id' type='hidden' value='255.255.255.255'>
</a>

    <script>
        function sendVar() {
            var ip= document.getElementById('ip_id').value;         
            alert(ip); 
        }
    </script>
</td>

Help me please.

Define the function only once, and pass this as an argument.

http://jsfiddle.net/nsy04nfr/1/

<script>
    function sendVar(elem) {
        alert(elem.firstElementChild.value); 
    }
</script>

<td>
<a href=''  onclick='sendVar(this)'>    
        Check 192.168.0.0
        <input id='ip_id' type='hidden' value='192.168.0.0'>
</a>

And unless you actually need input elements, I'd probably use data- attributes instead.

<script>
    function sendVar(elem) {
        alert(elem.getAttribute('data-ip')); 
    }
</script>

<a href='' data-ip='192.168.0.0' onclick='sendVar(this)'>    
        Check 192.168.0.0
</a>
(Something like this) Puedo sugerir algo como esto : 

Your function sendVar will live in a global context and can call from any point after is declared . In the onlick event of "a", first avoid the default behavior and then call the function by passing "this" at this point "this" is in fact "a", then in the function find child nodes of "a" of type "input ", as its just one,u can access it as the zero element , and take its "value" .

    <script type="text/javascript">
        function sendVar(ele) {
            var i = ele.getElementsByTagName("input");
            if (i && i[0]) {
                alert(i[0].value);
            }
        }
    </script>
    <table>
        <tr>
            <td>
                <a href = ''    onclick = 'event.preventDefault();
                        sendVar(this)' >
                    Check 192.168.0.0
                    <input type = 'hidden' value = '192.168.0.0' >
                </a>

            </td>
        <td>
            <a href = ''    onclick = 'event.preventDefault();
                    sendVar(this)' >
                Check 255.255.255.255
                <input type = 'hidden' value = '255.255.255.255' >
            </a>
        </td>
    </tr>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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