【发布时间】:2013-06-05 09:38:29
【问题描述】:
我有一个如下的javascript函数:
function calculateBill(id,price)
{
var qty = document.getElementById('qty_'+id).value;
var cur_value =qty*price;
var frm_lngth = document.getElementById('bnfsendgoods').length;
var fld_length1 = Number(frm_lngth) - 10;
var counter = document.getElementById('cntr').value;
var fld_length = (Number(fld_length1)) / (Number(counter));
fld_length = Number(fld_length);
var temp_total = 0;
alert(fld_length);
for(var i = 1; i<=fld_length; i++)
{
if( i != id )
{
alert('qty_'+i); //line 301,alerts only qty_1
var temp_q = document.getElementById('qty_'+i).value;
var temp_p = document.getElementById('ret_price_'+i).value; //Line 308
var temp_total1 = temp_q*temp_p;
temp_total = Number(temp_total) + Number(temp_total1);
}
}
var final_total = Number(cur_value) + Number(temp_total);
document.getElementById('total').value = final_total;
}
在第 301 行,alert(fld_length); 提醒 8 。如果假设 id = 3 ,根据我的逻辑,它应该
警报如qty_1、qty_2、qty_4、qty_5、qty_6 等。但它只提醒 qty_1 。怎么了?
【问题讨论】:
-
alert(fld_length);提醒什么?一些 HTML 代码也可能有助于发现问题。 -
有支持的 HTML 吗?
-
alert(fld_length);提醒8。 -
上面写着
Uncaught TypeError: Cannot read property 'value' of null test.html:308。相应的 html 字段就像<input type="hidden" name="ret_price_0" value="20" />、<input type="hidden" name="ret_price_1" value="20" />等。第 308 行我在上面标记了问题 -
您不应该在
Number()中包含以下变量:fld_length1、fld_length、temp_total、temp_total1、cur_value- 它们已经是数字。您可以使用qty、temp_q、temp_p来执行此操作,尽管在乘法中使用它们会隐式转换它们。
标签: javascript loops numbers