如何使用jQuery获取表中单元格的内容? [英] How to get the contents of a cell in a table using jQuery?

查看:80
本文介绍了如何使用jQuery获取表中单元格的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的Bootstrap表,

I have a Bootstrap table like this,

<table class="table table-striped table-bordered">
    <tr><th>First Name</th><th>Last Name</th><th>Age</th><th>Delete</th></tr>
    <tr><td>Mickey</td><td>Mouse</td><td>5</td><td><button type="submit" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-search"></span></button></td>
    <tr><td>Tom</td><td>Cat</td><td>6</td><td><button type="submit" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-search"></span></button></td>
    <tr><td>Pooh</td><td>Bear</td><td>4</td><td><button type="submit" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-search"></span></button></td>
    <tr><td>Donaled</td><td>Duck</td><td>7</td><td><button type="submit" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-search"></span></button></td>
    <tr><td>Jerry</td><td>Mouse</td><td>8</td><td><button type="submit" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-search"></span></button></td>
</table

当用户单击搜索按钮时,我想将名字(该行的第一个单元格)输出到另一个<div>.我们如何使用jQuery实现这一目标?

When a user clicks on a search button, I want to output the firstname, which is the first cell of that row to another <div>. How can we achieve this using jQuery?

这里是我到目前为止所做的链接- http://jsbin.com/xenapucisi/edit?html,输出

Here's a link to what I've done so far - http://jsbin.com/xenapucisi/edit?html,output

推荐答案

这应该可以解决问题.

$('button').click(function() {
  var tr = $(this).parents('tr');
  var name = $('td:first', tr).text();
  $('#output').html(name);
});

http://jsbin.com/gutivujewu/1/edit?html, js,输出

这篇关于如何使用jQuery获取表中单元格的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆