带有单选按钮选项的Javascript价格计算器
我正试图为客户编写一个非常简单的价格计算器。他们希望用户能够以英寸为单位输入宽度和长度,然后从一组单选按钮中选择产品以确定价格。最终计算结果为宽度x长度x价格。我可以让脚本计算宽度和长度,但我不确定如何从选中的单选按钮设置价格的var。提前感谢任何能帮上忙的人。我还想将结果限制在小数点后2位。代码如下带有单选按钮选项的Javascript价格计算器,javascript,Javascript,我正试图为客户编写一个非常简单的价格计算器。他们希望用户能够以英寸为单位输入宽度和长度,然后从一组单选按钮中选择产品以确定价格。最终计算结果为宽度x长度x价格。我可以让脚本计算宽度和长度,但我不确定如何从选中的单选按钮设置价格的var。提前感谢任何能帮上忙的人。我还想将结果限制在小数点后2位。代码如下 <html> <head> <title>Calculator</title> <script type="text/javascript
<html>
<head>
<title>Calculator</title>
<script type="text/javascript">
//calculator
function cal() {
var x=document.form.length.value
var y=document.form.width.value
var a=x*y
var p=document.form.radio.value
var total=a*p
document.form.total.value=total;
}
</script>
</head>
<body>
<h3>Project cost calculator:</h3>
<form action="" name="form">
<p><input type="text" name="length" size="4" />
Enter the length of your banner in inches.<br />
<input type="text" name="width" size="4" />
Enter the width of your banner in inches.</p>
<p>
<input type="radio" name="radio" id="paper" value="0.0625">
paper</label>
<input type="radio" name="radio" id="vinyl" value="0.08333333">
<label for="product">vinyl</label>
</p>
<p><b>Click this button to calculate the approximate cost of your project:</b><br />
<input type="button" name="money" value="calculate" onclick="cal()" /></p>
<p> <input type="text" name="total" size="6" /></p>
</form>
</body>
</html>
计算器
//计算器
函数cal(){
var x=document.form.length.value
变量y=document.form.width.value
变量a=x*y
var p=document.form.radio.value
var总计=a*p
document.form.total.value=总计;
}
项目成本计算器:
以英寸为单位输入横幅的长度。
输入横幅的宽度(以英寸为单位)
纸张
乙烯基
单击此按钮可计算项目的大致成本:
替换您的代码
var p=document.form.radio.value
与
更新:
上面的代码在IE中不起作用,因此,应该使用以下代码
if( document.getElementById("paper").checked == true ){
var p=document.getElementById("paper").value;
}
else{
var p=document.getElementById("vinyl").value;
}
请参阅中@Quentin的答案
if( document.getElementById("paper").checked == true ){
var p=document.getElementById("paper").value;
}
else{
var p=document.getElementById("vinyl").value;
}