0

我有 2 个 html 表。在第一个表中,我将能够选择所需的值并单击添加按钮,该按钮获取第一个表中的记录并将其附加到第二个表中。我在第一个表中也有一个 td,它由组合框组成,在组合框中选择值并单击添加按钮我将能够根据组合中的值将记录附加到第二个表意味着添加记录很多次。

我现在有一个疑问:

我在第二个表中有一个由数值组成的文本框,当我从第一个表中选择记录并添加时,该行会动态附加到第二个表并添加一个文本框。除此之外,我在这些表格之外还有一个文本框。应添加第二个表中文本框的值,并且应在不属于这些 html 表的文本框中显示总数。

与 jquery 一起的 php 代码示例如下:

php代码

     <div>

        <label>Todays date</label>

        <input type=text name=tdate id=tdate>// used a datepicker for this

        <input type=button class=addBtn>

     <table border=1 id=tbl>

      <tr>

          <th>sno</th>

          <th>name</th>

          <th>date</th>

          <th>insertion</th>

          <th>Amount</th>

     </tr>

     <tr>

         <td id=num>1</td>

         <td id=name>abc</th>

         <td id=date>08-10-2012</td>

         <td><select id=insertion name=insertion>

         <?php for($i=1;$i<=10;$i++) { echo "<option value=$i>$i</option>; }?>

         </select>
         </td>

         <td id=amount>10000</td>

    </tr>

   <tr>

     <td id=num>2</td>

     <td id=name>def</th>

     <td id=date>23-10-2013</td>

     <td>

      <select id=insertion name=insertion>

      <?php for($i=1;$i<=10;$i++) { echo "<option value=$i>$i</option>; }?>

     </select>

     </td>

     <td id=amount>10000</td>

   </tr>

     </table>

// 上面的表格数据是使用组合框的 onchange 事件从数据库中购买的,并且有很多数据,但是

//暂时不显示。

     <table border = 1 id = clone>

   <tr>

    <th>sno</th>

    <th>name</th>

    <th>date</th>

    <th>insertion</th>

    <th>Amount</th>

    <th>today date</th>

  <tr>

</table>


<label>totAmount</label>

<input type = text name= totAmount id=totAmount>

  </div>

函数.js

$('#tbl').on('click', 'tbody tr', function(event) { 

            if ($(this).hasClass('selected')) { 

                $(this).removeClass('selected');

                $(this).css("background", "white");

            } else {       

                $(this).addClass('selected');
                $(this).css("background", "#ffc");                    
            }
});


  var k=1;

  $(".addBtn").on('click', function(event) {

  if ($('#tbl tr').hasClass('selected')) {

      var items = [];

      $('#tbl tr.selected').each(function() {           

                     var name= $(this).find("#name").html();

                     var date = $(this).find("#date").html();

                     var insertion = $(this).find("#insertion option:selected").html();

         var amount = $(this).find("#amount").html();

         var todaydate = document.getElementById("tdate").value;

        for(var j=1; j<=insertion;j++) {

              var n = "<tr>"+"<td>"+k+"</td>"+"<td>"+name+"</td>"+

                  "<td>"+date+"</td>"+"<td>"+j+"</td>"+

                  "<td>"+amount+"</td>"+"<td>"+todaydate+"</td>"+

                    "</tr>";

                items.push(n);

                $("#clone").append(n);

                k++;                

          }
      });
    }
      $("#tbl tr").removeClass('selected');
      $("#tbl tr").css('background','white');
 });

// 我有文本框totAmount。

//我希望将克隆表中存在的金额添加到totAmount中的金额中,并且

// 当我单击添加按钮时,从表 (tbl) 中选择 tr 后,正在附加 tr

//但是无法将金额添加到文本框 totAmount.

我想知道这是从表中复制数据并将其附加到另一个表的最佳方法。

如果没有,请让我知道将数据从一个表复制到另一个表的任何其他可能方式,请举个例子。

谢谢

请帮忙。

4

0 回答 0