在 JS Input 字段中划分值并计算
Divide value in JS Input field and calculate
我是新手,我正在尝试构建成本计算器。
在输入字段 3 上,我希望值除以 80。因此,如果用户输入 80 作为值,它将计为 1。如果输入 81,则计为 2,依此类推。
你能帮忙吗?
这是我的代码:
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<script>
function updatesum() {
document.input.total.value = (document.input.input1.value -0) * (document.input.input2.value -0) * (document.input.input3value -0);
return false;
}
</script>
<body>
<form name="input" action="#" method="get" onsubmit="return false;">
Calculator<br>
Value1:<input type="text" name="input1" value=" "><br>
Value2:<input type="text" name="input2" value="2"><br>
Value3:<input type="text" name="input3" value=" "><br>
Costs:<input type="text" name="total" value=" ">
<input type="submit" value="Submit" onclick="updatesum()">
</form>
</body>
</html>
非常感谢!
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form name="input" action="#" method="get" onsubmit="return false;">
Calculator<br>
Value1:<input type="text" name="input1" value=""><br>
Value2:<input type="text" name="input2" value="2"><br>
Value3:<input type="text" name="input3" onblur="calcVal()" value=""><br>
Costs:<input type="text" name="total" value="">
<input type="button" value="Calculate" onclick="updatesum()">
</form>
<script type="text/javascript">
function updatesum() {
document.input.total.value = Math.ceil(document.input.input1.value / 80) * Math.ceil(document.input.input2.value / 80) * Math.ceil(document.input.input3.value / 80);
return false;
}
function calcVal() {
document.input.input3.value = Math.round(document.input.input3.value / 80) + 1;
}
</script>
</body>
</html>
这是最终答案(测试成功)。
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<script type="text/javascript">
function updatesum() {
document.input.total.value = Math.ceil(document.input.input1.value / 80) * Math.ceil(document.input.input2.value / 80) * Math.ceil(document.input.input3.value / 80);
return false;
}
</script>
<body>
<form name="input" action="#" method="get" onsubmit="return false;">
Calculator<br>
Value1:<input type="text" name="input1" value=""><br>
Value2:<input type="text" name="input2" value="2"><br>
Value3:<input type="text" name="input3" value=""><br>
Costs:<input type="text" name="total" value="">
<input type="button" value="Calculate" onclick="updatesum()">
</form>
</body>
</html>
我是新手,我正在尝试构建成本计算器。 在输入字段 3 上,我希望值除以 80。因此,如果用户输入 80 作为值,它将计为 1。如果输入 81,则计为 2,依此类推。 你能帮忙吗?
这是我的代码:
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<script>
function updatesum() {
document.input.total.value = (document.input.input1.value -0) * (document.input.input2.value -0) * (document.input.input3value -0);
return false;
}
</script>
<body>
<form name="input" action="#" method="get" onsubmit="return false;">
Calculator<br>
Value1:<input type="text" name="input1" value=" "><br>
Value2:<input type="text" name="input2" value="2"><br>
Value3:<input type="text" name="input3" value=" "><br>
Costs:<input type="text" name="total" value=" ">
<input type="submit" value="Submit" onclick="updatesum()">
</form>
</body>
</html>
非常感谢!
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form name="input" action="#" method="get" onsubmit="return false;">
Calculator<br>
Value1:<input type="text" name="input1" value=""><br>
Value2:<input type="text" name="input2" value="2"><br>
Value3:<input type="text" name="input3" onblur="calcVal()" value=""><br>
Costs:<input type="text" name="total" value="">
<input type="button" value="Calculate" onclick="updatesum()">
</form>
<script type="text/javascript">
function updatesum() {
document.input.total.value = Math.ceil(document.input.input1.value / 80) * Math.ceil(document.input.input2.value / 80) * Math.ceil(document.input.input3.value / 80);
return false;
}
function calcVal() {
document.input.input3.value = Math.round(document.input.input3.value / 80) + 1;
}
</script>
</body>
</html>
这是最终答案(测试成功)。
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<script type="text/javascript">
function updatesum() {
document.input.total.value = Math.ceil(document.input.input1.value / 80) * Math.ceil(document.input.input2.value / 80) * Math.ceil(document.input.input3.value / 80);
return false;
}
</script>
<body>
<form name="input" action="#" method="get" onsubmit="return false;">
Calculator<br>
Value1:<input type="text" name="input1" value=""><br>
Value2:<input type="text" name="input2" value="2"><br>
Value3:<input type="text" name="input3" value=""><br>
Costs:<input type="text" name="total" value="">
<input type="button" value="Calculate" onclick="updatesum()">
</form>
</body>
</html>