1

基本的に、5 つの異なる列と 10 行のテーブルがあります。誰かが をクリックしたときに、次のようにしたいと思いますrow

  • 行の最初の列のテキスト値を取得します
  • 以前に取得した値を警告する

私はすでに何かを試しましたが、実際の列のデータを取得しますが、これは私にとっては良くありません。

$('.table tbody tr').click( function (e) {
    alert ($(e.target).text());
} );

どんな助けでも大歓迎です。

4

5 に答える 5

2

これを試して:

$('.table tbody tr').click( function () {
    alert ($(this).find('td:first').text());
} );
于 2013-11-01T13:20:03.913 に答える
2

これを試して:

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
});    
于 2013-11-01T13:20:26.373 に答える
1

これはうまくいくはずです:

$('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)必要な他の子要素をターゲットにすることもできます。

例: http://jsfiddle.net/RxgwH/1/

于 2013-11-01T13:27:09.447 に答える
0

これにより、必要なものが得られます。

$('.table tbody tr').click( function () {
    alert($(this).children('td:first').text());
});
于 2013-11-01T13:45:50.783 に答える
0
$('.table tr td').click( function () {
    alert ($(this).text());
} );
于 2013-11-01T13:30:56.603 に答える