【问题标题】:jQuery Duplicate Table row and assign new id to duplicate table rowjQuery重复表行并将新ID分配给重复表行
【发布时间】:2017-11-24 18:22:39
【问题描述】:

我创建了一个包含多列的表,并编写了一个 jquery javascript 来复制或克隆表的最后一行。但是,当它克隆最后一行时,它还会为每一列赋予与前一行相同的名称和 ID。

jsp代码:

<div id="invTruck" class="invTruck">
            <table id="tbl_invTruck" width="100%"  border="0">
            <tbody>
                <tr>
                    <td width="15%" style="display:none;"><center>Work Order Id</center></td>
                    <td width="17%"><center>Truck Type</center></td>
                    <td width="17%"><center>Licences Plate #</center></td>
                    <td width="17%"><center>Driver ID</center></td>
                    <td width="17%"><center>Max Haulage Weight</center></td>
                    <td width="17%"><center>Job Number</center></td>
                </tr>

                <tr>
                    <td style="display:none;"><input name="wInv_work_Id" type="text"></td>
                    <td><select id="invTru_Type" name="invTru_Type" onchange="getTruckPlates(this.value)">
                        <option disabled selected hidden value="">Select A Truck Type</option>
                        <%while(rsinvTru1.next()){%>
                         <option><%=rsinvTru1.getString(1)%></option>
                        <%}%>
                        </select>
                    </td>
                    <td><select id="invTru_LicensePlateNo" name="invTru_LicensePlateNo" required>
                        <option disabled selected hidden value="">Select A Truck</option>

                        </select></td>
                    <td><input name="driver_emp_Id" type="text"></td>
                    <td><input name="invTru_MaxHw" type="text"></td>
                    <td><input name="" type="text"></td>
                </tr>
                </tbody>
            </table>
            <table width="100%" height="50%" border="0">
                <tr>
                    <td width="50%"><input class="buttonCreateInv" id="btn_AddTruck" type="button"  value="Add A Truck"></td>
                    <td width="50%"><input class="buttonCreateInv" name="btn_RemoveTruck" type="button" value="Remove A Truck"></td>
                </tr>
            </table>
            </div>

JQuery Javascript:

$(document).ready(function(){
    $("#btn_AddTruck").click(function(){
       var $tableBody = $('#tbl_invTruck').find("tbody"),
        $trLast = $tableBody.find("tr:last"),
        $trNew = $trLast.clone();

    $trLast.after($trNew); 

    });
});

我想要的预期输出是重复的表格行 其中 id1 中的 id 是原始表 td id 并附加了 1 。 如果我要在表格中添加另一行 其中 id2 中的 id 是原始表 td id 和 2 附加到它。

【问题讨论】:

    标签: javascript jquery html mysql clone


    【解决方案1】:

    尝试下一个:

     $(document).ready(function () {
                $("#btn_AddTruck").click(function () {
                   var $tableBody = $('#tbl_invTruck').find("tbody"),
                    $trLast = $tableBody.find("tr:last"),
                    $trNew = $trLast.clone();
                    // Find by attribute 'id'
                    $trNew.find('[id]').each(function () {
                        var num = this.id.replace(/\D/g, '');
                        if (!num) {
                            num = 0;
                        }
                        // Remove numbers by first regexp
                        this.id = this.id.replace(/\d/g, '') 
                            // increment number
                            + (1 + parseInt(num, 10));
                    });
            
                    $trLast.after($trNew); 
            
                });
            });
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <div id="invTruck" class="invTruck">
                    <table id="tbl_invTruck" width="100%"  border="0">
                    <tbody>
                        <tr>
                            <td width="15%" style="display:none;"><center>Work Order Id</center></td>
                            <td width="17%"><center>Truck Type</center></td>
                            <td width="17%"><center>Licences Plate #</center></td>
                            <td width="17%"><center>Driver ID</center></td>
                            <td width="17%"><center>Max Haulage Weight</center></td>
                            <td width="17%"><center>Job Number</center></td>
                        </tr>
    
                        <tr>
                            <td style="display:none;"><input name="wInv_work_Id" type="text"></td>
                            <td><select id="invTru_Type" name="invTru_Type" onchange="getTruckPlates(this.value)">
                                <option disabled selected hidden value="">Select A Truck Type</option>
                                <!-- %while(rsinvTru1.next()){%>
                                 <option><%=rsinvTru1.getString(1)%></option>
                                <%}% -->
                                </select>
                            </td>
                            <td><select id="invTru_LicensePlateNo" name="invTru_LicensePlateNo" required>
                                <option disabled selected hidden value="">Select A Truck</option>
    
                                </select></td>
                            <td><input name="driver_emp_Id" type="text"></td>
                            <td><input name="invTru_MaxHw" type="text"></td>
                            <td><input name="" type="text"></td>
                        </tr>
                        </tbody>
                    </table>
                    <table width="100%" height="50%" border="0">
                        <tr>
                            <td width="50%"><input class="buttonCreateInv" id="btn_AddTruck" type="button"  value="Add A Truck"></td>
                            <td width="50%"><input class="buttonCreateInv" name="btn_RemoveTruck" type="button" value="Remove A Truck"></td>
                        </tr>
                    </table>
                    </div>

    【讨论】:

    • 我试过这个方法但是它没有改变我得到 NaN 的克隆 ID 感谢这个解决方案你的时间和精力非常感谢
    • 我尝试了您的 jquery 解决方案,但它将克隆行的 id 设置为 NaN。非常感谢您的时间和精力非常感谢您
    • 别忘了暂时删除!----部分
    • 您好,我现在正在尝试将表格行中的每个 id 都增加 1,您能帮忙吗?您提供的代码仅将选择框增加 1。任何帮助将不胜感激。谢谢
    • 非常感谢,请忽略上次我已经编码了大约 36 个小时没有睡觉,忘记 id 不要给我的字段提供任何 id。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    相关资源
    最近更新 更多