Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JQuery在HTML中查找具有特定名称属性或id的元素_Javascript_Jquery_Css_Html - Fatal编程技术网

Javascript 使用JQuery在HTML中查找具有特定名称属性或id的元素

Javascript 使用JQuery在HTML中查找具有特定名称属性或id的元素,javascript,jquery,css,html,Javascript,Jquery,Css,Html,我有一个包含两列的表,其中一列是链接,单击它,我将调用jqueryclick事件。单击后,我想获取另一列的值,该列是该列的同级(td)。我怎样才能得到它 HTML <table class="table table-bordered table-hover"> <thead> <th>RID&l

我有一个包含两列的表,其中一列是链接,单击它,我将调用jqueryclick事件。单击后,我想获取另一列的值,该列是该列的同级(td)。我怎样才能得到它

HTML
<table class="table table-bordered table-hover">
                                        <thead>
                                            <th>RID</th>
                                            <th>Mobile Number</th>
                                            <th>Outlet Name</th>
                                            <th>Place</th>
                                            <th>Email</th>
                                            <th>Balance</th>
                                            <th>Stock</th>
                                            <th>Password</th>
                                        </thead>
                                        <tr>
                                            <td data-name="rid">5111879</td>
                                            <td data-name="mobile">9066587654</td>
                                            <td data-name="outlet">Rachels</td>
                                            <td>Indhiranagar, BL</td>
                                            <td>rach@yahoo.com</td>
                                            <td><i class="fa fa-inr"></i>378665</td>
                                            <td><a href="#" id="retailer_stock">TRANSFER</a></td>
                                            <td><a href="#" id="retailer_reset">RESET</a></td>
                                        </tr>                                                                                                                           
                                    </table>
在这些代码行中,最后两行不起作用,它不能给我一个结果,你们能解释为什么吗


-感谢使用jQuery中的Click事件:

捕获“零售商库存”的点击,然后您可以从“rid”中检索HTML

使用:

$('#retailer_stock').click( function(e){
    alert( $(this).closest('tr').children().eq(0).text() )
})
检查演示:

如果您想通过
数据名
属性访问该单元格,请使用:

$('#retailer_stock').click( function(e){
    alert( $(this).closest('tr').find('[data-name="rid"]').text() )
})
但是,如果您计划在表中有许多行,那么最终将有许多元素具有相同的
retailer\u stock
id。为了避免这种情况,您可以决定将
id=“retailer\u stock”
更改为
class=“retailer\u stock”
,然后将选择器更改为
$('.retailer\u stock')

$(文档).ready(函数(){
$(“#零售商库存”)。单击(功能(e){
警报($(this).closest('tr').find('td[data name=“rid”]')).text()
})
});
摆脱
手机号码
门店名称
放置
电子邮件
平衡
股票
密码
5111879
9066587654
轴
印地拉那加
rach@yahoo.com
378665
$(“#零售商_库存”)。单击(功能(e){
$(this).parent().find('[data name=“rid”]').text()

});Hi表示,,您可以点击id=retailer\u stock获取价值数据name='rid',就像点击获取价值“5111879”一样。。我说得对吗?只是出于好奇,你不能把id放到rid字段吗?第一个有效,但第二个无效,我的东西find()无效。请确保使用正确的语法。两种方法都可以显示两个连续的警报。确定吗?这里我得到了dat值$(this.nexist('tr')。find('td:eq(0)')。text()请试试这个
$('#retailer_stock').click( function(e){
    alert( $(this).closest('tr').find('[data-name="rid"]').text() )
})
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

<script>

    $(document).ready(function () {

        $('#retailer_stock').click(function (e) {
            alert($(this).closest('tr').find('td[data-name="rid"]').text())
        })

    });
</script>
</head>
<body>
<table class="table table-bordered table-hover">
                                        <thead>
                                            <th>RID</th>
                                            <th>Mobile Number</th>
                                            <th>Outlet Name</th>
                                            <th>Place</th>
                                            <th>Email</th>
                                            <th>Balance</th>
                                            <th>Stock</th>
                                            <th>Password</th>
                                        </thead>
                                        <tr>
                                            <td data-name="rid">5111879</td>
                                            <td data-name="mobile">9066587654</td>
                                            <td data-name="outlet">Rachels</td>
                                            <td>Indhiranagar, BL</td>
                                            <td>rach@yahoo.com</td>
                                            <td><i class="fa fa-inr"></i>378665</td>
                                            <td><a href="#" id="retailer_stock">TRANSFER</a></td>
                                            <td><a href="#" id="retailer_reset">RESET</a></td>
                                        </tr>                                                                                                                           
                                    </table>
</body>
</html>