for ($i=1; $i<=3; $i++)
{
print("<tr>");
print("<td>$i</td>");
print("<td><input type='text' name='meter[$i]' class='meter' maxlength='21' id='meter[$i]' /></td>");
print("<td><input type='text' name='weight[$i]' maxlength='21' class='weight' id='weight[$i]' size='10' /></td>");
print("<td><input type='text' name='amount[$i]' maxlength='21' class='amount' id='amount[$i]' size='10' /></td>");
print("</tr>");
}
?>
我想要这样
如果我输入110米和10重量它应该给我ans(10/110)* 100; 在每一栏
(weight/meter)*100
meter weight output 110 | 10 | 9.09 120 | 12 | 10 130 | 14 | 10.76
答案 0 :(得分:0)
一种方法是使用javascript代码,以便在浏览器的客户端本地完成计算,而无需向服务器提交表单:
<?php
print("<script>");
print("function calculate(index) {;");
print(" var meterd = document.getElementById('meter['+index+']');");
print(" var weightd = document.getElementById('weight['+index+']');");
print(" var amountd = document.getElementById('amount['+index+']');");
print(" if (meterd && weightd && amountd) {;");
print(" var meter = parseFloat(meterd.value); if (isNaN(meter)) {meter = 1;}");
print(" var weight = parseFloat(weightd.value); if (isNaN(weight)) {weight = 0;}");
print(" amountd.value = weight * 100 / meter;");
print(" };");
print("};");
print("</script>");
print("<table>");
for ($i=1; $i<=3; $i++)
{
print("<tr>");
print("<td>$i</td>");
print("<td><input type='text' name='meter[$i]' class='meter' maxlength='21' id='meter[$i]' onkeyup='calculate($i)'/></td>");
print("<td><input type='text' name='weight[$i]' maxlength='21' class='weight' id='weight[$i]' size='10' onkeyup='calculate($i)'/></td>");
print("<td><input type='text' name='amount[$i]' maxlength='21' class='amount' id='amount[$i]' size='10' /></td>");
print("</tr>");
}
print("</table>");
?>
经过测试,它对我有用。
答案 1 :(得分:0)
要只有2位小数,可以使用javascript value1.toFixed(2):
print(" var value1 = weight * 100 / meter;");
print(" amountd.value = value1.toFixed(2);");