想要计算表中的总行数

时间:2013-01-17 06:46:35

标签: php html jquery

我正在进行敲除绑定以将值插入表中。 如果表中有nuber的行为0,那么我必须显示一条警告消息。 但为此,我需要计算显示数据的行数。

我的观点代码如下:

    <table class="table table-hover table-bordered datagrid" style="width: 355px;" id="remodellingTable">
       <thead>
         <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Address</th>
            <th>City</th>
            <th>State</th>
         </tr>
       </thead>
       <tbody data-bind="foreach:items">
          <tr>
             <td data-bind="text:fname" id="tdfname"></td>
             <td data-bind="text:lname"></td>
             <td data-bind="text:addr"></td>
             <td data-bind="text:city"></td>
             <td data-bind="text:state"></td>
          </tr>
       </tbody>
    </table>
<button type="button" class="btn" id="nexttarget"> Next <i class="icon-arrow-right"></i></button>

我的jquery代码检查如下:

$('#nexttarget').live('click', function () 
{
     var rowcount = ('#remodellingTable > tbody > tr').length;
    if (rowcount > 0) 
    {
       $('#authorize').hide();
       $('#target').hide();
       $('#sideone').show();
    }
    else 
    {
       alert("There are no targets selected");
    }

}

2 个答案:

答案 0 :(得分:4)

您忘记使用$调用jQuery:

var rowcount = $('#remodellingTable > tbody > tr').length;

http://jsfiddle.net/ENQrX/

如果你不调用jQuery,你只需要评估字符串'#remodellingTable > tbody > tr'的长度,这显然是无关紧要的。

答案 1 :(得分:1)

使用此

var rowcount = $('#remodellingTable > tbody > tr').length;

而不是var rowcount = ('#remodellingTable > tbody > tr').length;

相关问题