请参阅下面的简单测试程序。在警报中,我需要显示带有两位小数的变量 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>
最佳答案
使用 alert("Price Entered="+ Number(PriceEntered).toFixed(2) + "和 max= "+ Number(MaxPrice).toFixed(2));
。
关于Javascript数字格式显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17254976/