检查null或0 [英] Check null or 0

查看:68
本文介绍了检查null或0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码创建总和:


预测=预测+ eval(f [i] .value);


这只适用于f [i] .value的情况。包含一个数字。


有人可以帮助我,以便它可以工作,如果f [i] .value

为空或包含文字。


问候,


S

I''m using the following code to create a sum:

forecast = forecast + eval(f[i].value);

This only works if "f[i].value" contains a number.

Can someone please help me so that it also works if "f[i].value" is
empty or contains text.

Regards,

S

推荐答案

sta ... @ gmail.com写道:
sta...@gmail.com wrote:
我正在使用以下代码创建一个总和:

预测=预测+ eval(f [i] .value);

这只适用于f [i] .value的情况。包含一个数字。

有人可以帮助我,以便它可以工作,如果f [i] .value是空的或包含文字。

问候,

S
I''m using the following code to create a sum:

forecast = forecast + eval(f[i].value);

This only works if "f[i].value" contains a number.

Can someone please help me so that it also works if "f[i].value" is
empty or contains text.

Regards,

S




试试这个:


预测=预测+ parseInt(f [i] .value);

http://www.w3schools.com/jsref/jsref_parseInt.asp


- JS



st **** @ gmail.com 写道:
我正在使用以下代码创建一个总和:

forecast = forecast + eval(f [i] .value);
I''m using the following code to create a sum:

forecast = forecast + eval(f[i].value);




使用eval经常被滥用和不必要。


请尝试以下方法来创建总和:

预测+ = + f [i] .value;



The use of eval is most often misused and unnecessary.

Try the following to create your sum:

forecast += +f[i].value;


于2006年2月27日在comp.lang.javascript中写道
wrote on 27 feb 2006 in comp.lang.javascript:
我正在使用以下代码创建总和:

预测=预测+ eval(f [i] .value);

只有在f [i] .value时才有效。包含一个数字。

有人可以帮助我,以便它可以工作,如果f [i] .value是空的还是包含文本。
I''m using the following code to create a sum:

forecast = forecast + eval(f[i].value);

This only works if "f[i].value" contains a number.

Can someone please help me so that it also works if "f[i].value" is
empty or contains text.




eval()是邪恶的,我认为你没有理由在这里使用它。


===========


如果您的意思是:


var forecast = 7

var xxx = 8

var yyy =''xxx''

预测=预测+ eval(yyy);

提醒(预测)// 15

使用起来要好得多:


var forecast = 7

var xxx = 8

var yyy =''xxx''

预测=预测+窗口[yyy];

警报(预测)// 15


现在假如xxx没有初始化或NaN:


var forecast = 7

var xxx

var yyy =''xxx''

if(!window [yyy] || isNaN(window [yyy])){

var a = 0

yyy =''a''

}

预测=预测+窗口[yyy];

提醒(预测)// 7


这会有帮助吗?

-

Evertjan 。

荷兰。

(请在我的电子邮件地址中将x'变为点数)



eval() is evil, and I see no reason why you are using it here.

===========

If you mean the following:

var forecast = 7
var xxx = 8
var yyy =''xxx''
forecast = forecast + eval(yyy);
alert(forecast) //15

it is much better to use:

var forecast = 7
var xxx = 8
var yyy =''xxx''
forecast = forecast + window[yyy];
alert(forecast) //15

now what if xxx is not initialized or NaN:

var forecast = 7
var xxx
var yyy = ''xxx''
if (!window[yyy]||isNaN(window[yyy])) {
var a = 0
yyy = ''a''
}
forecast = forecast + window[yyy];
alert(forecast) //7

will this help?
--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)


这篇关于检查null或0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆