简体   繁体   English

jQuery克隆表行(DATA)

[英]jQuery clone table row (DATA)

I have a table input form as per the picture. 我有一张表格输入图片。 I want users to be able to enter just the first row (hostname, model, location, purchased, warranty fields) and then be able to hit 'Clone First Row' and it copies what they've entered to all the subsequent rows. 我希望用户能够仅输入第一行(主机名,型号,位置,购买的产品,保修字段),然后单击“克隆第一行”,然后它将输入的内容复制到所有后续行中。

How best to do this in jQuery? 如何最好地做到这一点的jQuery?

The rows are being generated in PHP with this code, and all have unique IDs: 这些行是使用此代码在PHP中生成的,并且所有行都有唯一的ID:

<?php for($t = 1; $t <= 20; $t++){ ?>
        <tr>
            <td><input type="text" name="hostname-<?=$t;?>" class="form-control" id="hostname-<?=$t;?>"></td>
            <td><input type="text" name="asset-tag-<?=$t;?>" class="form-control" id="asset-tag-<?=$t;?>"></td>
            <td><input type="text" name="serial-<?=$t;?>" class="form-control" id="serial-<?=$t;?>"></td>

表

 $("button").on("click", function() { var firstRow = $("table").find("tr:first-child"), rowsToModify = firstRow.nextAll(); firstRow.find(":input").each(function(idx) { rowsToModify.find(":input:eq(" + idx + ")").val(this.value); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody> <tr> <td><input type="text" name="hostname-0" class="form-control" id="hostname-0"></td> <td><input type="text" name="asset-tag-0" class="form-control" id="asset-tag-0"></td> <td><input type="text" name="serial-0" class="form-control" id="serial-0"></td> <td> <select name="state-0" class="form-control" id="state-0"> <option>Open</option> <option>Waiting</option> <option>Closed</option> </select> </td> </tr> <tr> <td><input type="text" name="hostname-1" class="form-control" id="hostname-1"></td> <td><input type="text" name="asset-tag-1" class="form-control" id="asset-tag-1"></td> <td><input type="text" name="serial-1" class="form-control" id="serial-1"></td> <td> <select name="state-1" class="form-control" id="state-1"> <option>Open</option> <option>Waiting</option> <option>Closed</option> </select> </td> </tr> <tr> <td><input type="text" name="hostname-2" class="form-control" id="hostname-2"></td> <td><input type="text" name="asset-tag-2" class="form-control" id="asset-tag-2"></td> <td><input type="text" name="serial-2" class="form-control" id="serial-2"></td> <td> <select name="state-2" class="form-control" id="state-2"> <option>Open</option> <option>Waiting</option> <option>Closed</option> </select> </td> </tr> </tbody> </table> <button type="button">Clone values from first row</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM