突然,Netscape出现NaN错误 [英] Suddenly, a NaN error in Netscape
问题描述
嗨!
我已经修改了一个javascript订单表单计算脚本,自动计算小计,发货/处理和总计的
。
我做了一些修改,它仍然工作正常,即使在Netscape,
但在某些时候,运输/处理领域已经停止显示
在该浏览器中正确显示Not A Number错误。 (在Safari中它它看起来很好,并且它在我测试过的所有浏览器中都能正确计算
with。)
我不是java天才,只是一个复制粘贴和修补男孩,所以我已经基本不知道我搞砸了什么。我的细齿梳子已经转过来了,因为我无法理解为什么它会破裂。任何人都可以帮忙吗?
谢谢!
亚当
PS - 它确实在IE中表现得有点奇怪 - 我发现我需要点击
所有的小计字段才能让它们显示任何内容 - 但我想
'这只是一个java浏览器的Java怪癖... < < ad div*** @ shaw.ca>写道:
[snip]
我做了一些修改,它仍然工作正常,即使在Netscape,
但在某些指出装运/处理字段已停止在该浏览器中正确显示
,显示非数字错误。 (在Safari中它看起来很好,它可以在我测试过的所有浏览器中正确计算
。
http://wiseowlmultimedia.com/learnin...s_orderUS.html
我是没有java天才,只是一个复制粘贴和修补男孩,所以我基本上不知道我搞砸了什么。我的细齿梳已经转过来了,我无法理解为什么它会破裂。有人可以帮忙吗?
[snip]
你的代码中有很多可能的原因。因此,我认为重写脚本更容易
。这样,我还展示了一些更好的方法来完成你正在做的事情。
function totalItem(i,e){
/ *获取对表单控件的引用。 * /
var oP = e [''Item''+ i +''Price''],
oQ = e [''Item''+ i + ''数量''],
oT = e [''Item''+ i +''Total''];
/ *获取值并转换它们数字。 * /
var p = + oP.value,q = + oQ.value,t;
/ *检查价格和数量是否为正值,
*有限数。如果没有,请立即退出。 * /
if(isNaN(p)|| isNaN(q)|| p< 0 ||
q< 0 ||!isFinite(p)|| !isFinite(p)){return;}
/ *数量不应该是分数
*部分,所以让我们删除它。 * /
oQ.value = String(q = Math.floor(q));
/ *计算总数和舍入数。 * /
t = Math.round((p * 100)* q)/ 100;
/ *将总数转换为格式化的
*字符串并更新表单。 * /
oT.value = toCurrency(t,''
'','','');
返回t;
}
函数totalOrder(){
var e = document.forms [''orderform'']。元素,
n = 40,
t = 0,
s;
/ *循环项目1..n,计算项目
*总计并将其添加到订单总计中。 * /
for(var i = 1; i< = n; ++ i){
var x = totalItem(i,e);
/ *检查错误。 * /
if(''undefined''== typeof x){
alert(''Item''+ i +''包含无效值。\ n''+
''请更正它并再试一次。'');
e [''Item''+ i +''Quantity'']。焦点();
返回;
}
t + = x;
}
/ *小于
100,加15%;
Hi!
I''ve modified a javascript order form calculation script that tallies
up the subtotal, shipping/handling, and total automatically.
I did some modifications and it still worked fine, even in Netscape,
but at some point the Shipping/Handling field has ceased to display
correctly in that browser, showing a Not A Number error. (In Safari it
looks fine, and it calculates correctly in all browsers I''ve tested it
with.)
http://wiseowlmultimedia.com/learnin...s_orderUS.html
I''m no java genius, just a copy-paste-and-tinker boy, so I''ve
basically got no clue what I messed up. My fine-tooth comb has turned
up no reason I can see why it broke. Can anyone help?
Thanks!
Adam
PS - It does act a bit peculiar in IE - I found I needed to click on
all the subtotal fields to make them show anything - but I suppose
that''s just a java quirk of Explorer...
On 10 Aug 2004 14:17:33 -0700, Adam <ad********@shaw.ca> wrote:
[snip]
I did some modifications and it still worked fine, even in Netscape,
but at some point the Shipping/Handling field has ceased to display
correctly in that browser, showing a Not A Number error. (In Safari it
looks fine, and it calculates correctly in all browsers I''ve tested it
with.)
http://wiseowlmultimedia.com/learnin...s_orderUS.html
I''m no java genius, just a copy-paste-and-tinker boy, so I''ve
basically got no clue what I messed up. My fine-tooth comb has turned
up no reason I can see why it broke. Can anyone help?
[snip]
There are numerous possible causes in your code. Because of this, I
thought it easier to rewrite the script. That way, I also demonstrate some
more robust ways of accomplishing the things you were doing.
function totalItem(i, e) {
/* Get references to the form controls. */
var oP = e[''Item'' + i + ''Price''],
oQ = e[''Item'' + i + ''Quantity''],
oT = e[''Item'' + i + ''Total''];
/* Get the values and convert them to numbers. */
var p = +oP.value, q = +oQ.value, t;
/* Check that price and quantity are positive,
* finite numbers. If not, quit now. */
if(isNaN(p) || isNaN(q) || p < 0 ||
q < 0 || !isFinite(p) || !isFinite(p)) {return;}
/* A quantity shouldn''t have a fractional
* part, so let''s remove it. */
oQ.value = String(q = Math.floor(q));
/* Calculate total and round. */
t = Math.round((p * 100) * q) / 100;
/* Convert the total to a formatted
* string and update the form. */
oT.value = toCurrency(t, ''
'', '','');
return t;
}
function totalOrder() {
var e = document.forms[''orderform''].elements,
n = 40,
t = 0,
s;
/* Loop through items 1..n, computing the item
* total and adding it to the order total. */
for(var i = 1; i <= n; ++i) {
var x = totalItem(i, e);
/* Check for an error. */
if(''undefined'' == typeof x) {
alert(''Item '' + i + '' contains an invalid value.\n'' +
''Please correct it and try again.'');
e[''Item'' + i + ''Quantity''].focus();
return;
}
t += x;
}
/* Less than
100, add 15%;
这篇关于突然,Netscape出现NaN错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!