1

私はjqueryを初めて使用し、テーブルから既存の要素をカウントしたいのですが、html構造は次のようになります。

<table summary="">
    <tr>
       <td>
           <table id="myid" some more attributes> <-- i got that id and 
               <tbody>
                   <tr>foo</tr> <---counting
                   <tr>bar</tr> <---counting
               </tbody>
            </table>
         </td>
      </tr>
 </table>
HTML is apex gernerated.

jQueryからjqueryステートメントを試しました。テーブル内の行数を数えますが、何も機能しませんでした。私の機能していない「解決策」は

 $("table[id='myid'] > tbody >tr").length

また

 $('table#myid >tbody:last >tr').length

ヒントやコツに感謝します

マリオ

4

7 に答える 7

3
于 2012-08-30T13:48:43.143 に答える
2

try this.

http://jsfiddle.net/L7Nea/1/

JS

$('#myid tbody tr').length); 
于 2012-08-30T13:49:14.977 に答える
1

あなたがあなたの特定のテーブルの中を意味するなら

$('#myid tr').length // <-- this gets all tr inside table with id=myid

または、本当にdomをトラバースして、そのテーブル内に別のテーブルがある場合に備えて、より具体的にしたい場合

$('#myid > tbody > tr').length

さらに、すでにIDセレクターを使用しているので、持っている必要はありませんtable#myid

于 2012-08-30T13:48:15.870 に答える
1

What you have is what you should use, just with the right id:

$("table[id='myid'] > tbody > tr").length

or

$("table#myid > tbody > tr").length

Demo: http://jsfiddle.net/Guffa/bbV3Z/

As the id should be unique in the page, you should be able to use just:

$("#myid > tbody > tr").length

The reason for using #myid > tbody > tr instead of just #myid tr is to make sure that you only get the rows in that specific table, and not rows from tables nested inside it. If you don't have any tables nested inside it, you can just use:

$("#myid tr").length
于 2012-08-30T13:52:27.240 に答える
0

You can use $("#table_id tr").length

于 2012-08-30T13:49:34.460 に答える
0

Use var count = $('#myid tr').length;.

于 2012-08-30T13:49:47.477 に答える
0

Think about performances also by takeing the context first :

$('#myid').find('tr')

here's the perfs comparisons : http://jsperf.com/tr-number

于 2012-08-30T13:57:34.600 に答える