简体   繁体   English

在td JavaScript中获取隐藏的输入值

[英]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? 所有的ID都相同。是否可以指定条件:印刷机中td中的INPUT ID? Trying to make it through the "this." 试图通过“这个”。 - but failed. -但是失败了。 Here my code: http://jsfiddle.net/nsy04nfr/ 这是我的代码: 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. 仅定义一次函数,并将this作为参数传递。

http://jsfiddle.net/nsy04nfr/1/ 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. 除非您实际上需要输入元素,否则我可能会改用data- attribute。

<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 . 您的函数sendVar将存在于全局上下文中,并且可以在声明之后从任意点调用。 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" . 在“ a”的onlick事件中,首先避免默认行为,然后在此时通过传递“ this”来调用函数,“ this”实际上是“ a”,然后在函数中找到类型为“ a”的子节点“输入”仅作为一个输入,u可以将其作为零元素进行访问,并获取其“值”。

    <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>

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

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