jQuery按类查找单击按钮的名称
jQuery find name of clicked button by class
我有三个带按钮的表,它们有相同的名称类,比如这个
<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>
我需要当我点击其中一个按钮时,我得到跨度的值。例如,如果我点击第一个按钮,我得到这个值。一些文本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()
JSFIDDLE
试试这个。。
cart = $(this).parents("table").find('.info').text();
而不是
cart=$(".info").text();
我建议:
$('.howdy').click(function(){
var cart = $(this).closest('table').find('.info').text();
console.log(cart);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>
你最初的jQuery的问题是:
$(".howdy",this).click(function(e) {
// you were selecting without any context, so retrieving
// all the .info elements, but the getter use of text()
// retrieves from only the first element of the collection:
cart=$(".info").text();
console.log(cart);
});
另外:$('.howdy', this)
是一个上下文选择器,在上下文this
中查找.howdy
元素。在这种情况下,this
(可能)指的是全局对象window
,在这里工作(或者应该工作,具体取决于this
是什么),但可能会引起问题。
顺便说一句,上面的替代方案是:
$('.howdy').click(function(){
var cart = $('.info').eq($(this).index('.howdy')).text();
console.log(cart);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>
但对于这样一项简单的任务来说,这有点过于复杂了。
参考文献:
- CCD_ 11
eq()
find()
index()
要在按钮之前获取最接近的信息,您还可以向上移动并获取行的prev()同级-这样您就可以在同一个表中拥有行
$(".howdy").click(function(e) {
var cart=$(this).closest("tr").prev().find(".info").text();
window.console&&console.log(cart);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<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>
相关文章:
- 如何隐藏按钮单击事件上的占位符
- 从Web服务器下载图像按钮单击使用JavaScript
- 在SweetAlert中传递ASP.NET按钮单击事件
- 单选按钮单击可刷新/更改网站的小区域
- 调用按钮单击事件 ajax 加载的用户控件
- 附加 jQuery 代码以在加载和按钮单击时运行
- 在按钮单击时将图像URL数据显示到弹出框中,而无需禁用背景
- 按钮单击服务器单击
- 触发单选按钮单击,并在页面刷新时记住选择
- 如何使用 javascript 而不是使用控制器中的方法在 rails 中呈现部分按钮单击
- jQuery UI 选项卡 - 将参数设置为下一个/上一个按钮单击
- 如何在 Angular JS 中的按钮单击上添加类
- 当元素上有多个类时触发按钮单击事件
- 如何在 HTML 中的按钮单击上更改图像(IMG URL 每次来自服务器)
- 如何使用javascript刷新我的html页面时清除按钮单击
- 阻止后退按钮 - 单击按钮时忽略
- 如何制作按钮.单击“开始时不运行”
- Rails:尝试在按钮单击时渲染部分
- 在页面加载时调用函数,在按钮单击时再次调用函数,但参数不同
- 如何在服务器端的文本中添加按钮单击事件