Javascript 使用JQuery在HTML中查找具有特定名称属性或id的元素
我有一个包含两列的表,其中一列是链接,单击它,我将调用jqueryclick事件。单击后,我想获取另一列的值,该列是该列的同级(td)。我怎样才能得到它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
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>