使用jQuery更改特定Div标签内的TD值

时间:2016-01-26 07:59:18

标签: javascript php jquery html html-table

我有以下HTML表格,

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

当我点击ID为Discount的按钮时,我需要将值更改为div标签内的TD,其中id为#34; total&#34;并将其值设置为另一个JavaScript变量?我尝试了以下内容,但它无效。

 $(".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");


}); 

2 个答案:

答案 0 :(得分:1)

尝试使用我的代码来帮助您

将HTML <div id="totalone">$875.00</div>更改为$<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.
}); 
});

答案 1 :(得分:0)

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