Q:使用javascript计算两个输入
我已经创建了以下代码,在最后3个输入上,我想用INPUT2计算INPUT1并自动得到INPUT3的结果 目前,ofc不起作用,但如果我将输入3改为突然起作用,但ofc无法将任何数据发布到数据库中。如何在不使用span标记的情况下输出计算结果 这是代码Q:使用javascript计算两个输入,javascript,php,html,Javascript,Php,Html,我已经创建了以下代码,在最后3个输入上,我想用INPUT2计算INPUT1并自动得到INPUT3的结果 目前,ofc不起作用,但如果我将输入3改为突然起作用,但ofc无法将任何数据发布到数据库中。如何在不使用span标记的情况下输出计算结果 这是代码 <script> function divideBy() { num1 = document.getElementById("firstNumber").value; num2 =
<script>
function divideBy()
{
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
document.getElementById("result").innerHTML = num2 % num1;
}
</script>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Create new Route</h2>
</div>
<p>Enter the route<i><strong>"YYYY-MM-DD</i></p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($reason_err)) ? 'has-error' : ''; ?>">
<label>Reason</label>
<input type="text" name="reason" class="form-control" value="<?php echo $reason; ?>">
<span class="help-block"><?php echo $reason_err;?></span>
</div>
<div class="form-group <?php echo (!empty($startdest_err)) ? 'has-error' : ''; ?>">
<label>Start Destination</label>
<input type="text" name="startdest" class="form-control" value="<?php echo $startdest; ?>">
<span class="help-block"><?php echo $startdest_err;?></span>
</div>
<div class="form-group <?php echo (!empty($enddest_err)) ? 'has-error' : ''; ?>">
<label>End Destination</label>
<input type="text" name="enddest" class="form-control" value="<?php echo $enddest; ?>">
<span class="help-block"><?php echo $enddest_err;?></span>
</div>
<div class="form-group <?php echo (!empty($startkm_err)) ? 'has-error' : ''; ?>">
<label>INPUT 1</label>
<input type="text" name="startkm" class="form-control" id="firstNumber" value="<?php echo $startkm; ?>">
<span class="help-block"><?php echo $startkm_err;?></span>
</div>
<div class="form-group <?php echo (!empty($endkm_err)) ? 'has-error' : ''; ?>">
<label>INPUT 2</label>
<input type="text" name="endkm" class="form-control" id="secondNumber" onChange="divideBy()" value="<?php echo $endkm; ?>">
<span class="help-block"><?php echo $endkm_err;?></span>
</div>
<div class="form-group <?php echo (!empty($totalkm_err)) ? 'has-error' : ''; ?>">
<label>INPUT3</label>
<input type="text" name="totalkm" id="result" class="form-control" value="<?php echo $totalkm; ?>">
<span class="help-block"><?php echo $totalkm_err;?></span>
</div>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="start.php" class="btn btn-default">Cancel</a>
</form>
If i change it from:
<div class="form-group <?php echo (!empty($totalkm_err)) ? 'has-error' : ''; ?>">
<label>Total Km</label>
<input type="text" name="totalkm" id="result" class="form-control" value="<?php echo $totalkm; ?>">
<span class="help-block"><?php echo $totalkm_err;?></span>
</div>
To this it works but ofc as I said no data is posted:
<div class="form-group <?php echo (!empty($totalkm_err)) ? 'has-error' : ''; ?>">
<label>Total Km</label>
<span type="text" name="totalkm" id="result" class="form-control" value="<?php echo $totalkm; ?>">
<span class="help-block"><?php echo $totalkm_err;?></span>
</div>```
函数divideBy()
{
num1=document.getElementById(“firstNumber”).value;
num2=document.getElementById(“secondNumber”).value;
document.getElementById(“结果”).innerHTML=num2%num1;
}
创建新路线
输入路线“YYYY-MM-DD
@KIKO软件我在读了几遍你的评论后解决了:)更改了“document.getElementById(“result”).value”的代码,它现在解决了这个问题,在更改最后一个输入时,它会自动计算前面提到的两个输入的结果,这就是我要查找的结果
<script>
function divideBy()
{
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
document.getElementById("result").value = num2 % num1;
}
</script>
函数divideBy()
{
num1=document.getElementById(“firstNumber”).value;
num2=document.getElementById(“secondNumber”).value;
document.getElementById(“结果”).value=num2%num1;
}
感谢您的帮助:)是否要更改document.getElementById(“结果”)的值
?而不是innerHTML
?你的意思是当页面加载input1+input2,然后将值放入第三个输入中时???@Milad Yes:)可能吗?@KIKOSoftware对不起,先生,你能解释一下吗?:)@StudioMan当然,只要调用脚本标记内的函数divideBy()
我很高兴它能起作用,我不确定它能起作用。好吧,但这给我留下了一个问题,你显然知道,读取值需要
,那么你为什么要尝试使用.innerHTML
来编写值?@Dark absol对此的简短回答:我是初学者:)