Json 数据计算和 html

Json data calculation and to html

我是 Javascript 的新手。事实上,它有一个非常简单的答案,但我不能:(

价格显示javascript代码;

 $('#totalPricefark').html(data.totalPrice);

Html

<strong id="totalPricefark">0</strong>

我想做什么;

data.totalPrice * 0.6 计算并显示在#totalPricefark

谢谢

这从 priceFark div 中获取值,通过 pareseInt 函数将其解析为整数,然后乘以 0.6 并将数据填充到另一个元素中。

该解决方案使用了 jQuery,因为您的原始问题也使用了它。

试试这个:

var priceFark = parseFloat($("#totalPricefark").text());

$('#totalPricefark2').html(priceFark * 0.6);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong id="totalPricefark">2</strong>
<br/>
<strong id="totalPricefark2">1</strong>

你可以这样做

const data = {
  totalPrice: 10
};

$('#totalPricefark').text(data.totalPrice * 0.6);

试试这个

 var data={};
 data.totalPrice=15;
 var result=(parseFloat( data.totalPrice) * 0.6);
 $('#totalPricefark').html(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<strong id="totalPricefark">0</strong>

问题没有用javascript解决:(一个新的Json对象变量是用php创建的。它是这样解决的。感谢我的朋友们的帮助。