使用jquery中的每个新操作在一个div中显示每个div总金额

时间:2016-09-21 06:49:24

标签: javascript php jquery html

Javascript代码:

cities_find

我只想在一个div中添加两个动作。其中名为 total-amt method(:find_city) 在此div中,每当我点击任何操作时,总计应显示 total-amt div然后,如果我点击其他行动然后它的总金额也加上以前的金额。

1 个答案:

答案 0 :(得分:1)

试试这个,当你点击任何一个动作时,它将从该div获取前一个值,将其转换为整数(parseInt)然后它将在其中添加newValue。最后,它会再次将新的金额加入该div中。

修改-1

您需要从div中删除$ string。试试这个工作片段。

//Action performed and Add the sum to that div
$('a').click(function(){
  //New value that is going to add into div
  var newValueToAdd = 10;
  //Replace the HTML with final summation
  $('.total-amt')
    .html("$" + 
          (parseInt($('.total-amt').html().replace('$', '')) + 
          newValueToAdd));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="total-amt">$0</div>
<a href="javascript:void(0)">Add</a>