【发布时间】:2013-11-29 09:20:02
【问题描述】:
我正在使用三个 javascript 函数来求和输入字段值。但是前两个功能运行良好,但第三个功能不起作用。
这是我的 javascripts:
<script language="javascript">
function addNumbers()
{
var val1 = parseInt(document.getElementById("ot").value);
var val2 = parseInt(40);
var ansD = document.getElementById("totalot");
ansD.value = val1 * val2;
}
function addSecincome(){
var val1 = parseInt(document.getElementById("totalot").value);
var val2 = parseInt(document.getElementById("mc").value);
var val3 = parseInt("1024");
var ansD = document.getElementById("secincome");
ansD.value = val3 * val1 + val2;
}
function addIncome(){
var val1 = parseInt(document.form.getElementById("totalot").value);
var val2 = parseInt("150");
var ansD = document.form.getElementById("INC");
ansD.value = val2 * val1 ;
}
</script>
Here is my html codes:
<tr height="20">
<td height="20">BUYER</td>
<td colspan="3"><input type="text" name="buyer" value=" " /></td>
<td>OT</td>
<td><input id="ot" type="text" name="ot" value="" /></td>
<td><input type="text" id="totalot" name="totalot" onclick="javascript:addNumbers()" value="" /></td>
<td>MC</td>
<td><input id="mc" type="text" name="mc" value="" /></td>
<td colspan="2"><input id="secincome" type="text" onclick="javascript:addSecincome()" name="2ndincome" value="" /></td>
<td>SMV</td>
<td><input type="text" name="smv" value= " " /></td>
<td>O/QTY</td>
<td><input type="text" name="oqyt" value=" " /></td>
<td>TTL AVG</td>
<td><input type="text" name="planeffi" value=" " /></td>
</tr>
<tr height="20">
<td height="20">DAY INPUT</td>
<td colspan="3"><input type="text" name="dayinput" value=" " /></td>
<td colspan="2">PLAN HOUR</td>
<td> <input type="text" name="planhour" vaue="10" /></td>
<td colspan="2">PRO/LOSS</td>
<td colspan="2"><input id="ploss" type="text" onclick="javascript:pp();" name="proloss" value="" /></td>
<td>TGT</td>
<td>T/TGT</td>
<td>CUTTING</td>
<td><input type="text" name="cutting" value=" " /></td>
<td rowspan="2"><input type="text" name="ttlavg" value=" " /></td>
<td> </td>
</tr>
虽然函数语法相同,但第三个函数对我不起作用。我尝试过使用不同的输入 ID,但它对我不起作用。但前两个功能正在工作。我不明白这个问题,我以前没有这种问题。
需要您的帮助,谢谢。
【问题讨论】:
-
您是如何考虑使用
document.form.getElementById而没有意识到这是您的工作功能和失败的功能之间的区别?此外,当事情没有按照您想要的方式运行时,检查您的控制台是否有错误应该是您采取的第一步。 -
我没有看到你在哪里调用最后一个函数,另外两个是一些表格元素的点击处理程序。
-
为什么你必须使用 parseInt 从……一个整数创建一个整数?甚至从“150”开始?
-
getElementById 在表单对象上它的文档对象方法。好的...并在 Int 中解析 150 ?
标签: javascript html