我有 3 个带有按钮的表格,它们具有相同的名称类,如下所示
<table>
<tr><td><span class="info">Some Text 1</span></td> </tr>
<tr><td><button class="howdy">print out</button></td></tr>
</table>
<table>
<tr><td><span class="info">Some Text 2</span></td></tr>
<tr><td><button class="howdy">print out</button></td></tr>
</table>
<table>
<tr><td><span class="info">Some Text 3</span></td></tr>
<tr><td><button class="howdy">print out</button></td></tr>
</table>
我需要当我点击其中一个按钮时我得到span的值,例如如果我点击第一个按钮我得到这个值一些文本1我使用了一些jquery代码但在结果中他们给了我所有值
$(".howdy",this).click(function(e) {
cart=$(".info").text();
console.log(cart);
});
最佳答案
试试这个
$('.howdy').on('click', function(){
console.log($(this).closest('table').find('.info').text());
});
您从 $('.howdy')
开始遍历按钮并寻找最近的 <table>
那么您正在寻找.info
类里面并得到 .text()
关于javascript - jQuery 按类查找单击按钮的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26571829/