使用数字增量将值复制到next

时间:2018-01-05 11:40:05

标签: html-table copy cell

我有一个表格,其中包含从ajax()生成的单元格。最终的HTML表就是这样的例子:

<table id="sendMailTable">
  <thead>
    <tr>
      <th scope="col" style="width: 75px; text-align: center;">Table ID</th>
      <th scope="col" style="width: 100px;">Dated</th>
      <th scope="col" style="width: 140px;">RL/Tkt No</th>
    </tr>
  </thead>
  <tbody name="tblBody" id="tblBody">
    <tr>
      <td style="width: 75px; text-align: center;">1</td>
      <td style="text-align: center; vertical-align:middle;">04/12/2017</td>
      <td tabindex="1" autofocus="true" style="contenteditable=" true "></td>
        </tr>
        <tr>
            <td style="width: 75px; text-align: center; ">2</td>
            <td style="text-align: center; vertical-align:middle; ">04/12/2017</td>
            <td tabindex="1 " autofocus="true " autofocus="true "style="contenteditable="true"></td>
    </tr>
    <tr>
      <td style="width: 75px; text-align: center;">3</td>
      <td style="text-align: center; vertical-align:middle;">04/12/2017</td>
      <td tabindex="1" autofocus="true" style="contenteditable=" true "></td>
        </tr>
        <tr>
            <td style="width: 75px; text-align: center; ">4</td>
            <td style="text-align: center; vertical-align:middle; ">04/12/2017</td>
            <td tabindex="1 " autofocus="true " style="contenteditable="true"></td>
    </tr>
    <tr>
      <td style="width: 75px; text-align: center;">5</td>
      <td style="text-align: center; vertical-align:middle;">04/12/2017</td>
      <td tabindex="1" autofocus="true" style="contenteditable=" true "></td>
        </tr>
    </tbody>
</table>

最后一列的单元格可能只包含数字值或字母数字值。有没有办法用前一个单元格中的递增数值和任何文本值填充最后一列的所有后续单元格?与EK123,EK124或110,111,112等一样。但是,值只能从当前聚焦单元格或仅<td>更改。

JS代码如下:

$('.rlNum').bind("keyup", function(e) {
                       //on letter number
                        var IncrementedValue = '';
                       if (e.which <= 90 && e.which >= 48)
                       {
                        IncrementedValue  = IncrementedValue + String.fromCharCode(e.which);
                        for (i = rowClicked +1; i <= totalRows; i++) { 
                            if (e.which <= 57 && e.which >= 48){
                                IncrementedValue = (Number($(this).text().replace( /^\D+/g, '')) + 1).toString();
                             }else if(e.which >= 65 && e.which <= 90){
                                IncrementedValue = $(this).text();
                             }
                            var ele = $(this).parent().next().find('.rlNum');
                            window.alert( $(this).text());
                            ele.html(IncrementedValue);
                        }
                       }
                });

The unsolved jsfiddle is at this link

1 个答案:

答案 0 :(得分:0)

经过这么长时间的努力,Yp得到了它。 js代码如下:

    $(document).on("click", "#sendMailTable tbody tr", function() {
        currentRow = ($(this).index()+1);
    });

    $('.rlNum').bind("keyup", function(e) {
                    var currentCellInput = $(this).text();
                    $(this).find('tr').click();
                    totalRows = $('table >tbody >tr').length;
                    for (m = currentRow + 1, j=1; m <= totalRows; m++,j++) {    
                        document.getElementById('sendMailTable').rows[m].cells[6].innerHTML = processStr(currentCellInput,j);   
                    }   
                });

    function processStr(str, x) {
    var result = str.match(/[^-_ \d]+|\d+/g);
    for (i = 0; i <= result.length-1; i++) {
        if (!isNaN(result[i])){
            result[i]= ((Number(result[i])) + x).toString();
        }  
    }
    result.toString();
    return (result.join(""));
}