简体   繁体   English

克隆表格行

[英]Cloning a table row

I have the following table 我有下表

<table id="customFields" class="table table-bordered table-hover additionalMargin alignment">
    <thead>
    <tr>
        <th colspan="2"></th>
        <th>Some Title</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td><label class="subjectline" for="User1">User NOC M1</label></td>
            <td id="slLabel">SLA</td>
            <td id="slInput"><input type="text" name="slOptions[User][NOC M1]" class="form-control" id="User1"></td>
            <td><a class="addCF" href="javascript:void(0);">+ additional user</a></td>
        </tr> 
    </tbody>
</table>

I then have the following javascript to add additional rows 然后,我将使用以下JavaScript来添加其他行

$(function() {
    $(".addCF").click(function(){
        $("#customFields").append('<tr><td></td><td>SL_B</td> <td><input type="text" name="slOptions[User][NOC M1]" class="form-control" id="User1"></td> <td> &nbsp; <a href="javascript:void(0);" class="remCF">Remove</a></td></tr>');
    });
    $("#customFields").on('click','.remCF',function(){
        $(this).parent().parent().remove();
    });
});

This currently works how I want it to. 目前这是我想要的方式。 However, there are a couple of things I am having issues with. 但是,我遇到了一些问题。

Firstly, when you first view it, you will see the label SL_A. 首先,当您第一次查看它时,您会看到标签SL_A。 In the cloned version, I manually set it to SL_B. 在克隆的版本中,我手动将其设置为SL_B。 All other clones then have SL_B. 然后,所有其他克隆都具有SL_B。 What I am trying to do is have SL_ followed by the next letter in the alphabet. 我想做的是让SL_后跟字母表中的下一个字母。 So the third row should be SL_C. 因此,第三行应为SL_C。 I am not too sure how I can achieve this. 我不太确定如何实现这一目标。

My second issue relates to the name of the cloned input. 我的第二个问题与克隆输入的名称有关。 At the moment, they all have the same name eg slOptions[User][NOC M1] When a new row is added, the name should change to something unique, maybe using the additional letter of the alphabet above eg slOptions[User][NOC M1B] 目前,它们都具有相同的名称,例如slOptions[User][NOC M1]当添加新行时,名称应更改为唯一的名称,可能使用上面的字母附加字母,例如slOptions[User][NOC M1B]

Would it be possible to achieve these things? 有可能实现这些目标吗?

I have set up a Fiddle for demonstration 我设置了一个小提琴进行示范

Thanks 谢谢

Here is your solution for both the issues: See: https://jsfiddle.net/pdxgrpqz/ 这是您针对这两个问题的解决方案:请参阅: https//jsfiddle.net/pdxgrpqz/

$(function() {
    alp = "A";
    $(".addCF").click(function(){
    alp = (alp.substring(0,alp.length-1)+String.fromCharCode(alp.charCodeAt(alp.length-1)+1));
    $("#customFields").append('<tr><td></td><td>SL_'+alp+'</td> <td><input type="text" name="slOptions[User][NOC M1'+alp+']" class="form-control" id="User1"></td> <td> &nbsp; <a href="javascript:void(0);" class="remCF">Remove</a></td></tr>');
    });
    $("#customFields").on('click','.remCF',function(){
        $(this).parent().parent().remove();
    });
});

You could store a reference to the possible letters as well as your current letter and then within your function determine the appropriate one to use : 您可以存储对可能字母以及当前字母的引用,然后在函数中确定要使用的适当字母:

// Store the possible letters
var possibleLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
// Store the current letter
var currentLetter = 'A';

$(function() {
    $(".addCF").click(function(){
        // Resolve the next letter to add
        var nextIndex = possibleLetters.indexOf(currentLetter) + 1;
        // Update your reference
        currentLetter = possibleLetters[nextIndex];
        // Append it
        $("#customFields").append('<tr><td></td><td>SL_' + currentLetter + '</td> <td><input type="text" name="slOptions[User][NOC M1' + currentLetter + ']"...');
        // More code omitted for brevity
    });
    // Still more code omitted for brevity
});

You can see an example of this in action here and demonstrated below : 您可以在这里看到一个实际的示例,并在下面进行了演示:

在此处输入图片说明

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

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