1

誰かがテーブルの [ダウンロード] ボタンをクリックしたときに、その特定の行の日付値を関数に渡したいと考えています。現在、最初のテーブル行からの日付値のみを渡すことができます。

これは私が使用しているセレクタです:

$(this).parent().find('#period-start');

常に返されるもの:

[<td id=​"period-start">​5/1/2013​&lt;/td>]

親、子、検索、および最も近いセレクターの組み合わせを試しましたが、現在の行から日付値を取得するための正しいセレクターに出くわすことができませんでした。ありがとう。

テーブル

<table id="tblStatements" class="table">
            <thead>
                <tr>
                    <th>Period Starting</th>
                    <th>Period Ending</th>
                    <th>Download</th>
                </tr>
            </thead>
            <tbody>

<tr>
  <td id='period-start'>5/1/2013</td>
  <td id='period-end'>5/31/2013</td>
  <td><button type='submit'>Download</button></td>
</tr>

<tr>
  <td id='period-start'>4/1/2013</td>
  <td id='period-end'>4/30/2013</td>
  <td><button type='submit'>Download</button></td>
</tr>

<tr>
  <td id='period-start'>3/1/2013</td>
  <td id='period-end'>3/31/2013</td>
  <td><button type='submit'>Download</button></td>
</tr>

</tbody>
</table>
4

5 に答える 5

7

IDは常にHTML 内で一意であると想定されています。したがって、ID を使用せずにこれを試すことができます。

// Get the first td
var periodStart = $(this).closest('tr').children('td:eq(0)').text();  

// Get the second td
var periodEnd = $(this).closest('tr').children('td:eq(1)').text();  

フィドルのデモ

于 2013-06-26T09:37:56.303 に答える
2

これを使用してください -ただし、重複する ID を使用しないでください。代わりにクラスを使用してください

$(this).closest('tr').find('#period-start');

また -

$(this).closest('td').siblings('#period-start');
于 2013-06-26T09:29:29.733 に答える
1

これを試して

 $(this).parent().siblings('#period-start');

すべてのIDが一意であることを確認してください..HTMLが無効です....クラスに変更して、これを試してください

  $(this).parent().siblings('.period-start');
于 2013-06-26T09:31:30.877 に答える
1

同じ id use class insidead を持つ複数の要素を使用しないでください。

<tr>
  <td class='period-start'>5/1/2013</td>
  <td class='period-end'>5/31/2013</td>
  <td><button type='submit'>Download</button></td>
</tr>

<tr>
  <td class='period-start'>4/1/2013</td>
  <td class='period-end'>4/30/2013</td>
  <td><button type='submit'>Download</button></td>
</tr>

<tr>
  <td class='period-start'>3/1/2013</td>
  <td class='period-end'>3/31/2013</td>
  <td><button type='submit'>Download</button></td>
</tr>

このコードを使用して適切な要素を選択します

$(this).parents('tr').find('.period-start');
于 2013-06-26T09:32:20.297 に答える
0

これを試して

$.trim($(this).closest('tr').find('[id="period-start"]').html());//this gives start date
于 2013-06-26T09:35:12.213 に答える