<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
var form = document.forms.myform,
qty = form.qty,
cost = form.cost,
output = form.textbox;
window.calculate = function () {
var q = parseInt(qty.value, 10) || 0,
c = parseFloat(cost.value) || 0;
output.value = (q * c).toFixed(2);
};
</script>
</head>
<body>
<form action="caltest.php" method="post" name="myform" onkeyup="calculate()">
<label>Num of PAX :</label>
<input type="text" name="qty" />
<input type="hidden" name="cost" value="700" />
<br/>
<lable>Total Price: </lable>
<input type="text" name="textbox" />
</form>
</body>
</html>
我正在做一个简单的计算。没有得到任何输出。
答案 0 :(得分:1)
执行var form = document.forms.myform
时,还没有<form name="myform"
......
最简单的(尽管可能不是最复杂的)解决方案是在表单代码之后将<script>
块从<head>
移动到。
答案 1 :(得分:0)
试试这个正在运作
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="caltest.php" method="post" name="myform" onkeyup="calculate()" >
<label>Num of PAX :</label>
<input type="text" name="qty" />
<input type="hidden" name="cost" value="700" />
<br/>
<lable>Total Price: </lable>
<input type="text" name="textbox" />
</form>
<script>
var form = document.forms.myform,
qty = form.qty,
cost = form.cost,
output = form.textbox;
window.calculate = function () {
var q = parseInt(qty.value, 10) || 0,
c = parseFloat(cost.value) || 0;
output.value = (q * c).toFixed(2);
};
</script>
</body>
</html>