【问题标题】:Change the TD value inside a particular Div Tag inside using jQuery使用 jQuery 更改特定 Div 标签内的 TD 值
【发布时间】:2016-05-02 17:46:51
【问题描述】:

我有以下 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 的按钮时,我需要将值更改为 ID 为“total”的 div 标签内的 TD 并将其值设置为另一个 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");


}); 

【问题讨论】:

  • 两个元素有相同的id。
  • 更新了代码。对不起
  • 你能把所有的都贴出来吗??喜欢var test=$("#items #disc .dis").val(); ??
  • 代码再次更新 :) @PlatinumIndustries
  • 这里给两个不同元素的“折扣”id,修复它

标签: javascript php jquery html html-table


【解决方案1】:

试试我的代码对你有帮助

将 HTML &lt;div id="totalone"&gt;$875.00&lt;/div&gt; 更改为 $&lt;span id="totalone"&gt;875.00&lt;/span&gt;

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

【讨论】:

  • 它不工作。我把 console.log 一直放在最后的答案上,数学是正确的。但是,这行 $('#items #totalone').html("$"+tot);不工作。
  • @Ashif Shereef 这对我有用,请查看jsfiddle.net/wq9jnfwa
  • 运气不好 :(。发生了什么事。
  • 你实际需要什么,比如新总计 = 旧总计 - 减去值
  • 是的。它是一个减法运算
【解决方案2】:
$('#items #totalone').html("$"+new_tot);

【讨论】:

  • total_up 是 TR 类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多