简体   繁体   中英

Change the TD value inside a particular Div Tag inside using jQuery

I have the following HTML Table,

<table id="items">
   <tr class="total_up">

          <td colspan="2" class="blank"> </td>
          <td colspan="2" class="total-line">Total</td>

        <td class="total-value" id="total"><div id="totalone">$875.00</div></td> 
      </tr>

       <tr class="disc" id="disc">

          <td colspan="2" class="blank"> </td>
          <td colspan="2" class="total-line">Discount</td>
          <td class="total-value" id="discount"><div id="discountid"><input type="text" name="disco" class="dis"/></div> </td>
      </tr>



      <tr class="tax_up">
          <td colspan="2" class="blank"> </td>
          <td colspan="2" class="total-line balance">tax</td>
          <td class="total-value" id="tax"><div id="tax">00</div></td>
      </tr>

</table>

When i click on the button with id Discount, I need to change the value to the TD inside the div Tag with id "total" and set its value to another JavaScript variable?I tried the following, but it's not working.

 $(".discountbtn").click(function(){

var test=$("#items #disc .dis").val(); //Easiest method


    console.log("lol");
    console.log(test);
    var tot = roundNumber(test,2);

    var new_tot=window.finale-tot;
    console.log(window.finale);

    console.log(new_tot);

    $('#items #totalone').html("$"+new_tot);

   //alert("button");


}); 

Try with my code that help you

change HTML <div id="totalone">$875.00</div> to $<span id="totalone">875.00</span>

$(document).ready(function() {
    $("#discountbtn").click(function(){
        var test=$("#items #disc .dis").val(); 
        console.log(test);

        var oldTotal = $("#totalone").text();
        console.log(oldTotal);

        var tot = Math.round(test * 100) / 100;
        var new_tot=parseFloat(oldTotal)-tot;
        console.log(new_tot);

        $('#items #total').html(new_tot); //It was a jQuery selector glitch.
    }); 
});
$('#items #totalone').html("$"+new_tot);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM