如何在表中动态添加2行

时间:2012-04-08 12:22:07

标签: javascript jquery

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

以下是截图:

enter image description here 这是我的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>

但当然没有任何反应。你对这个问题有任何想法吗?

谢谢:)

3 个答案:

答案 0 :(得分:2)

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

var newContentRow = $('<tr>' + jourVar + '' + creneauVar + '' + repassageVar + '' + menageVar + '' + totalVar + '' + pttcVar + '' + corVar + ');
在连续结束时,有一个额外的+'

它应该是probalby:

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

编辑:

另外我应该提一下,每次单击链接时,您使用的rowNumber变量都不会向上迭代,因为每次单击时它都会重置。要么使用全局变量,要么只是在每次单击按钮时从表中获取行数,而不是如果要使用行号更新标题行。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

有很多问题。

正如所指出的,在连接行时,你错过了结束'

但是,你也试图连接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++;
});