jQuery动态表行数

时间:2016-03-08 10:34:34

标签: javascript jquery html-table

我的表格如下

<div class="row">
    <input type="button" id="btnAddGatePass" value="Add Gate Pass Requester" />
    <div class="table-responsive">
        <table id="gatePass" class="table table-striped table-hover" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Nationality</th>
                    <th>Job Title</th>
                    <th>Passport No.</th>
                    <th></th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>
</div>

并点击添加点击我在函数下面调用

$("#gatePass tbody").append(
        "<tr>" +
        "<td><input type='text' id='txtName' class='form-control'/></td>" +
        "<td><input type='text' id='txtNationality' class='form-control'/></td>" +
        "<td><input type='text' id='txtJobTitle' class='form-control'/></td>" +
        "<td><input type='text' id='txtPassportNo' class='form-control'/></td>" +
        "<td><span class='glyphicon glyphicon-floppy-disk btnSave forestgreen'>&nbsp;</span><span class='glyphicon glyphicon-trash btnDelete black'>&nbsp;</span></td>" +
        "</tr>");

$(".btnSave").bind("click", Save);
$(".btnDelete").bind("click", Delete);

点击提交按钮点击我正在计算行但总是给我零可能是什么问题

if ($('#gatePass >tbody >tr').length === 0) {
        return false;
    }

1 个答案:

答案 0 :(得分:2)

请尝试以下代码: -

var rowCount = $('#gatePass tr').length;