当table只有一行时,tableToJson不起作用

时间:2016-10-19 16:02:02

标签: javascript jquery html json

我正在使用here

中的tableToJson

我有以下模态窗口:

<div class="modal fade" id="myModalShopOrder" role="dialog">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Add Batch</h4>
                </div>
                <div class="modal-body">
                    <label class="control-label">Designation:</label>
                    <div id='complaintProgress' style="position: relative;">
                        <input type="text" class="form-control" name="DesignationShopOrder" id="DesignationShopOrder">
                    </div><br>

                    <div class="well">
                    <table class="table table-hover table-striped table-responsive table-bordered" id="shopOrderTable" >
                        <thead>
                            <tr>
                                <th data-override="ShopOrderDes">Designation</th>
                            </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                        </div>
                </div>
                <div class="modal-footer">                              
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                    <button type="button" onclick="addShopOrder();" class="btn btn-success">Submit</button>
                </div>
            </form> 
        </div>
    </div>
</div>

我正在运行的脚本将项添加到表中:

<script>
    function addShopOrder(){
        var designation = document.getElementById('DesignationShopOrder').value;
        var table = document.getElementById("shopOrderTable").getElementsByTagName('tbody')[0];
        var row = table.insertRow(-1);
        row.hidden = false;
        var cell1 = row.insertCell(0);
        cell1.innerHTML = designation;

        var shopOrders = $('#shopOrderTable').tableToJSON();
        shopOrders.pop();
        var infoRec = new Array();
        infoRec = JSON.stringify(shopOrders);
        document.getElementById('shopOrderSender').value = infoRec;
    }

</script>

我点击按钮添加,我的tableToJson数组为空! 如果我点击两次,我会得到包含数据的数组!

为什么我需要点击两次?如果我在表格中有一行,我也想转换它! 红色矩形内有萤火虫细节的图像! No Data after first click

With one element after second click

提前致谢

1 个答案:

答案 0 :(得分:3)

原因是“shopOrders.pop();”如果您只有一个项目正在移除它,那么您要移除最后一个项目