基本的に、5 つの異なる列と 10 行のテーブルがあります。誰かが をクリックしたときに、次のようにしたいと思いますrow
。
- 行の最初の列のテキスト値を取得します
- 以前に取得した値を警告する
私はすでに何かを試しましたが、実際の列のデータを取得しますが、これは私にとっては良くありません。
$('.table tbody tr').click( function (e) {
alert ($(e.target).text());
} );
どんな助けでも大歓迎です。
基本的に、5 つの異なる列と 10 行のテーブルがあります。誰かが をクリックしたときに、次のようにしたいと思いますrow
。
私はすでに何かを試しましたが、実際の列のデータを取得しますが、これは私にとっては良くありません。
$('.table tbody tr').click( function (e) {
alert ($(e.target).text());
} );
どんな助けでも大歓迎です。
これを試して:
$('.table tbody tr').click( function () {
alert ($(this).find('td:first').text());
} );
これを試して:
var previousValue = ''
$('.table tbody tr').click( function (e) {
alert(previousValue);
previousValue = $(this).find('td:first').text());
});
2番目の値を取得するにはどうすればよいですか
eq()
セット内の特定のアイテムを取得するには、ゼロベースの を使用できます。
var previousValue = ''
$('.table tbody tr').click( function (e) {
alert(previousValue);
previousValue = $(this).find('td').eq(1).text()); // second item
});
これはうまくいくはずです:
$('tr').each(function() {
$(this).click(function() {
var selectedTd = $(this).children('td:first-child').text();
alert(selectedTd);
});
});
で置き換えtd:first-child
て:nth-child(1)
から、 のように番号を増やして、:nth-child(2)
必要な他の子要素をターゲットにすることもできます。
これにより、必要なものが得られます。
$('.table tbody tr').click( function () {
alert($(this).children('td:first').text());
});
$('.table tr td').click( function () {
alert ($(this).text());
} );