1

数値の一番上の行を持つ大きなテーブル (id=PersonValue) があり、すべての行の最初のセルは名前です。テーブル内の特定のセルをクリックすると、その行の最初のセルの名前とその列の一番上のセルの数値を取得したいと考えています。

このスクリプトで名前を取得し、それに応じて調整しましたが、名前がわかりません。

$('#PersonValue').click(function(){
    var name = $(this).closest('tr').find('td:first').text();
    console.log('Name is '+ name); // returns 'Name is ' while something like 'Name is Niko' is expected

  //var number = ??
});

そして、スクリプトでわかるように、一番上のセルを取得する方法がわかりません。

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

4

3 に答える 3

1

完全な実用的なソリューションは次のとおりです。

$('table').on('click', 'td', function() {
    var $this = $(this),
        name = $this.siblings(':first-child').text(),
        pos = $this.index() + 1,
        num = $this.parent().siblings().first().children(':nth-child('+pos+')').text();

    alert(num + ' & ' + name);
});
于 2013-01-30T12:34:45.673 に答える
0
 $('#PersonValue tr').on("click","td",function() {

        var $td=$(this);
        var $tr=$td.parent();
        var tdindex=$td.prevAll().length;
        //or try $td.index();

        var name = $tr.find('td:first-child').text();
        var colheader =  $('#PersonValue').find("thead > th:nth-child("+tdindex+")").text(); 
 })

それをテストしておらず、あなたのhtml(theadのth?)も見ていませんが、あなたは私が望むアイデアを手に入れました。編集:テーブルtrから委任しながらインデックスメソッドを試してください

編集:インデックスを実際に良くするためのこのprevallメソッド

于 2013-01-30T12:21:50.730 に答える
0

次のコード ブロックを使用してみてください。

$('#PersonValue').click(function(){
    var name = $(this).siblings('tr').find('td:first-child').text();
    console.log('Name is '+ name); // returns 'Name is ' while something like 'Name is Niko' is expected

  //var number = ??
});

siblings()セレクターを使用して追加しました:first-child

于 2013-01-30T12:14:08.937 に答える