簡體   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