简体   繁体   English

Json数据计算和html

[英]Json data calculation and to html

I'm new to Javascript. 我是Java的新手。 In fact it has a very simple answer but I can't :( 实际上,它有一个非常简单的答案,但我不能:(

Price display javascript code; 价格显示javascript代码;

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

Html HTML

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

What I want to do; 我想做的事;

data.totalPrice * 0.6 calculation and show in #totalPricefark data.totalPrice * 0.6计算并显示在#totalPricefark中

Thanks 谢谢

This takes the value from the priceFark div, parses it into an integer via pareseInt function, and multiplies by 0.6 and populates the data into the other element. 这从priceFark div中获取值,通过pareseInt函数将其解析为整数,然后乘以0.6,然后将数据填充到另一个元素中。

The solution uses jQuery as your original question was using it as well. 该解决方案使用jQuery,因为您最初的问题也正在使用它。

Try this: 尝试这个:

 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> 

you could just do something like this 你可以做这样的事情

const data = {
  totalPrice: 10
};

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

try this 尝试这个

  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不能解决问题:(用php创建了一个新的Json对象变量。用这种方法解决了。感谢我的朋友们的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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