繁体   English   中英

如何在表格中动态添加2行

[英]How to add 2 rows dynamically in a table

我是 JS 和 jQuery 的初学者,我想在单击链接(页面底部的灰色矩形)时动态添加 2 行(橙色行和包含 cols 的行)。

这是一个截图:

在此处输入图像描述 这是我的 HTML 代码:

<div class="ajout_prest">
    <p class="txt_ajout">
        <a class="AddResults" href="#">Ajouter une nouvelle prestation</a>
    </p>
    <p class="plus_ajout">
        <a class="AddResults" href="#">+</a>
    </p>
</div>

和JS代码:

<script>
    $('.AddResults').click(function(){
        var rowNumber = 3;

        // All the cols
        var jourVar = $('<td class="jr_td"><p><input type="text" class="datepicker" /></p><p class="ou">ou</p><p><input type="text" class="datepicker" /></p></td>') ;
        var creneauVar = $('<td class="cr_td"><select><option value="h1">10h30</option><option value="h2">11h30</option></select><select class="cr_td_s2"><option value="h3">10h30</option><option value="h4">11h30</option></select></td>') ;
        var repassageVar = $('<td class="rp_td"><select><option value="h5" SELECTED>2h00</option><option value="h6">3h00</option></select></td>') ;
        var menageVar = $('<td class="mn_td"><select><option value="h7" SELECTED>2h00</option><option value="h8">3h00</option></select></td>') ;
        var totalVar = $('<td class="tt_td"><strong>4h.</strong></td>') ;
        var pttcVar = $('<td class="pttc_td"><strong>88 &#8364;</strong></td>') ;
        var corVar = $('<td class="cor_td"><a href="#"><img src="img/ico/corbeille.png" alt="" width="13" height="13" /></a></td>') ;      

        //Create 2 new rows
        var newTitreRow = $('<tr><td class="bar_td" colspan=7><strong>PRESTATION ' + rowNumber + '</strong></td></tr>') ;
        var newContentRow = $('<tr>' + jourVar + '' + creneauVar + '' + repassageVar + '' + menageVar + '' + totalVar + '' + pttcVar + '' + corVar + ');

        //Append the new row to the body of the #table_prest table
        $('#table_prest tbody').append(newTitreRow);
        $('#table_prest tbody').append(newContentRow);

        //Iterate row number
        rowNumber++;
    });
</script>

但是当然什么也没有发生。 你知道这个问题吗?

谢谢:)

您的 javascript 代码至少有一个错误:

var newContentRow = $('<tr>' + jourVar + '' + creneauVar + '' + repassageVar + '' + menageVar + '' + totalVar + '' + pttcVar + '' + corVar + ');

在连接的末尾有一个额外的 + '

它应该是:

var newContentRow = $('<tr>' + jourVar + '' + creneauVar + '' + repassageVar + '' + menageVar + '' + totalVar + '' + pttcVar + '' + corVar + '</tr>');

编辑:

另外我应该提到,您使用的 rowNumber 变量不会在您每次单击链接时向上迭代,因为它会在您每次单击时重置。 为此使用全局变量,或者如果您想使用行号更新标题行,则每次单击按钮时只获取表格中的行数。

我认为问题是你忘记了下面一行中的最后一个单引号 '

  var newContentRow = $('<tr>' + jourVar + '' + creneauVar + '' + repassageVar + '' + menageVar + '' + totalVar + '' + pttcVar + '' + corVar );

有很多问题。

正如所指出的,您在连接行时缺少结束符'

而且,您正在尝试连接 jQuery 个对象而不是字符串。 您根本不需要 jQuery 对象。

此外,您不需要在变量串联之间使用+ '' +

var rowNumber = 3;

$('.AddResults').click(function(){

    // Concatenate the cells into a single string
    var cells = 
      '<td class="jr_td"><p><input type="text" class="datepicker" /></p><p class="ou">ou</p><p><input type="text" class="datepicker" /></p></td>' +
      '<td class="cr_td"><select><option value="h1">10h30</option><option value="h2">11h30</option></select><select class="cr_td_s2"><option value="h3">10h30</option><option value="h4">11h30</option></select></td>' +
      '<td class="rp_td"><select><option value="h5" SELECTED>2h00</option><option value="h6">3h00</option></select></td>' +
      '<td class="mn_td"><select><option value="h7" SELECTED>2h00</option><option value="h8">3h00</option></select></td>' +
      '<td class="tt_td"><strong>4h.</strong></td>' +
      '<td class="pttc_td"><strong>88 &#8364;</strong></td>' +
      '<td class="cor_td"><a href="#"><img src="img/ico/corbeille.png" alt="" width="13" height="13" /></a></td>'

    // Create 2 new rows
    var newTitreRow = '<tr><td class="bar_td" colspan=7><strong>PRESTATION ' + rowNumber + '</strong></td></tr>'
    var newContentRow = '<tr>' + cells + '<tr>'

    //Append the new row to the body of the #table_prest table
    $('#table_prest tbody').append(newTitreRow + newContentRow);

    //Iterate row number
    rowNumber++;
});

暂无
暂无

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

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