请参阅下面的简单测试程序。在警报中,我需要显示带有两位小数的变量MaxPrice。我在测试程序中放置了$ Maxprice,但实际上,$ MaxPrice是从SQL文件中读取的,是一个带有数值的数字字段(在本例中为2.00)。警报仅显示" 2"。如何让它显示" 2.00"?
<html>
<head>
<script language="javascript">
function myFunction()
{
PriceEntered=document.forms["form1"]["Price"].value;
MaxPrice=document.forms["form1"]["MaxPrice"].value;
alert("Price Entered=" + PriceEntered + " and maximum= " + MaxPrice );
}
</script>
</head>
<?php
$MaxPrice=2.00;
?>
<body>
<form id="form1" name="form1" method="post" onsubmit="myFunction ()" action="test.php">
<input name="MaxPrice" id="MaxPrice" type="hidden" value="<?php echo $MaxPrice;?>">
<input name="Price" id="Price" type="text" value="3.00">
<input type="submit" name="submit1" value"Submit" />
</form>
</body>
答案 0 :(得分:1)
使用alert("Price Entered=" + Number(PriceEntered).toFixed(2) + " and maximum= " + Number(MaxPrice).toFixed(2));
。
答案 1 :(得分:1)
我的猜测是echo
并没有真正回显2.00而只是2,所以你可能想要调查一下。可能相关:http://php.net/manual/en/function.number-format.php
但是,从JavaScript的角度来看,尝试使用toFixed
方法,例如
MaxPrice = parseFloat(MaxPrice).toFixed(2);
答案 2 :(得分:0)
试试这个
var num = 2;
alert(num.toFixed(2));