gpt4 book ai didi

javascript - jquery 简单的计算脚本需要很少的帮助

转载 作者:太空宇宙 更新时间:2023-11-04 14:18:51 25 4
gpt4 key购买 nike

我在 friend 的帮助下制作了这个 jquery 脚本。

fiddle http://jsfiddle.net/Wu7k7/

j查询:

$('input[name=rdOptin]').click(function(){
var selected=$(this).val();
var var0 = 0.3;
var var1 = 20;
var var2 = var1 * 3.9 / 100;
var var3 = 20;
var var4 = (var1 + var3) * 3.9 / 100;
if (selected == 'norm'){
$("#calc_value").html(var0 + var1 + var2);}
else {
$("#calc_value").html(var0 + var1 + var3 + var4);}

});

html 表单

<form id="form1" name="form1" method="post" action="form1.php"> <b> Option 1:</b>
<input type="radio" name="rdOptin" value="norm" checked="checked" />Normal
<input type="radio" name="rdOptin" value="exp" />Expert
<br />
<b>Calculated Value:</b>
<div class="valueDisp">
<div id="calc_value">
select options
</div>
</div>
  1. 我试图将文本元素添加到选择的输出中。例如说:在选择“Expert” radio 时,输出是“41.86”,我怎样才能使这个输出看起来像“Numbers 41.86”。

  2. 选择“正常” radio 时,输出为“21.080000000000002”,我如何将其修复为“21.08”?

这对我来说相当棘手。

请 friend 帮忙。

非常感谢。

最佳答案

使用.toFixed()将 float 转换为固定精度的十进制字符串。此外,将您的计算与演示分开,以避免重复:

var result;
if (selected == 'norm'){
result = var0 + var1 + var2;
} else {
result = var0 + var1 + var3 + var4;
}

$('#calc_value').text("Numbers " + result.toFixed(2));

关于javascript - jquery 简单的计算脚本需要很少的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17828679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com