Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

javascript - How to get table row data in different table when selecting a checkbox?

I have a table in which some fields are given, like Service, Amount, tax, Action, When click on checkbox I want the all table row data could show on a different table and when I select the other checkbox I want this row data also should add below it in a table.

<table cellspacing="0" rules="all" border="1" id="" style="border-collapse:collapse;">
    <tbody>
        <tr>
            <th scope="col">Service </th>
            <th scope="col">Amount</th>
            <th scope="col">tax</th>
            <th scope="col">Action</th>
        </tr>
        <tr>
            <td>
                <span>Subscription Charges</span>
            </td>
            <td>
                <span>500.00</span>
            </td>
            <td>
                <span >90.00</span>
            </td>
            <td >
                <input type="checkbox" data-toggle="checkbox" class="tot_amount" value="590.00"  /> 
            </td>
        </tr>
        <tr>
            <td>
                <span>registration fees</span>
            </td>
            <td>
                <span>200.00</span>
            </td>
            <td >
                <span >80.00</span>
            </td>
            <td >
                <input type="checkbox" data-toggle="checkbox" class="tot_amount" value="590.00"  /> 
            </td>
        </tr>
    </tbody>
</table>

<table cellspacing="0" rules="all" border="1" id="" style="border-collapse:collapse;">
    <tbody>
        <tr>
            <th scope="col">Service </th>
            <th scope="col">Amount</th>
            <th scope="col">tax</th>
            <th scope="col">Action</th>
        </tr>
    </tbody>    
</table>
<input type="text" name="tot_amount">
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

$(document).ready(function(){
  $(document).on("change",".tot_amount",function(){  
  if (this.checked){  
     var servicetext = $(this).closest("tr").find('td').eq(0).find('span').text();
     var totalamt = $('#tot_amount').val();
     if(totalamt != ''){
     $('#tot_amount').val(totalamt + ", " + servicetext); 
     }
     else{
     $('#tot_amount').val(servicetext);
     }  
     
      var $tr = $(this).closest("tr");
      $("#table2 tbody").append("<tr class='servicetr'>" + $tr.html() + "</tr>");
  }  
  else{
   var $tr = $(this).closest("tr");  
   $('#table2').find(".servicetr").each(function(){    
     if($tr.html() == $(this).html())
     $(this).remove();  
   });
  
  }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table cellspacing="0" rules="all" border="1" id="table1" style="border-collapse:collapse;">
    <tbody>

         <tr>
        <th scope="col">Service </th>
        <th scope="col">Amount</th>
        <th scope="col">tax</th>
        <th scope="col">Action</th>
        </tr>

        <tr class='servicetr'>
                <td class="service">
                <span>Subscription Charges</span>
                 </td>
                 <td>
                <span>500.00</span>
                 </td>
                 <td class="service">
                <span >90.00</span>
                 </td>
                 <td >
              <input type="checkbox" data-toggle="checkbox" class="tot_amount" value="590.00"  /> 
                 </td>
      </tr>

      <tr class='servicetr'>
                <td>
                <span>registration fees</span>
                 </td>
                 <td >
                <span>200.00</span>
                 </td>
                 <td >
                <span >80.00</span>
                 </td>
                 <td >
              <input type="checkbox" data-toggle="checkbox" class="tot_amount" value="590.00"  /> 
                 </td>
      </tr>
    </tbody>
</table>

<br/>

<table cellspacing="0" rules="all" border="1" id="table2" style="border-collapse:collapse;">
    <tbody>

         <tr>
        <th scope="col">Service </th>
        <th scope="col">Amount</th>
        <th scope="col">tax</th>
        <th scope="col">Action</th>
        </tr>

    </tbody>    
</table>
<br/>
<input type="text" name="tot_amount" id="tot_amount" style="width:100%;">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...