在Javascript中未定义且为null [英] Undefined and null in Javascript

查看:56
本文介绍了在Javascript中未定义且为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在开发一个Web应用程序,其中包括使用DHTML,Java

和Javascript。

它根据数据库的内容(结果集)填充网页,

,每行旁边都有一个复选框(v1),允许选择该行

更改(例如删除,更新等)

所以我们正在创建一个复选框数组,对吗?

当然我必须检查是否存在这些复选框和如果有任何

被选中(选中)

使用Javascript,然后允许在Java中进行任何数据库操作。


现在它成为很奇怪。

我注意到如果有一排[即... 1复选框v1],该功能

始终显示警告并返回false:

function isAnySelected(checkbox)//检查是否至少有一个复选框是

选择

{

for(i = 0; i< checkbox.length; i ++){

if(checkbox [i]。已检查){

返回true;

}

alert("您必须至少选择一条记录。);

返回false;

}

为什么?我将其称为:onClick =" if(!isAnySelected(v1))return false;"

我试图修复该错误,并注意到在单行的情况下

checkbox.length是未定义。

所以我将该函数修改为:


函数isAnySelected(复选框)// OK:检查是否至少有一个复选框是

选择

{

if(checkbox.length =" undefined"){// with single' '=''!!!

if(checkbox.checked){

返回true;

}

} else {

for(i = 0; i< checkbox.length; i ++){

if(checkbox [i] .checked){

返回true;

}

}

}

alert("你必须选择至少一个记录。);

返回false;

}


这里我无法理解的是为什么这个功能仅当有一个

单个''=''时才有效:

if(checkbox.length =" undef '但是')... ???

检查null会产生Javascript错误或什么都不做。


如果有0,我怎么能让它工作行[因此0个实例

复选框v1?]


提前谢谢,

Oleg。

Hi,

I am working on a web application which among other things uses DHTML, Java
and Javascript.
It populates web page based on the contents of the database (resultset),
and next to each row there is a checkbox (v1) allowing to select that row
for changes (e.g. delete, update, etc.)
So we are creating an array of checkbox, correct ?
Of course I have to check whether any of these checkboxes exist and if any
of them got selected (checked)
using Javascript before allowing any DB operation in Java.

Now it becomes strange.
I have noticed that if there is 1 row [i.e. 1 checkbox v1], that function
always shows warning and returns false:
function isAnySelected(checkbox) //check if at least one checkbox is
selected
{
for(i=0; i<checkbox.length; i++) {
if(checkbox[i].checked) {
return true;
}
alert("You must choose at least one record.");
return false;
}
Why ? I call it as: onClick="if(! isAnySelected(v1)) return false;"
I tried to fix that error, and noticed that in the case of a single row
checkbox.length is "undefined".
So I modified that function into:

function isAnySelected(checkbox) //OK: check if at least one checkbox is
selected
{
if(checkbox.length = "undefined") { // with single ''='' !!!
if(checkbox.checked) {
return true;
}
} else {
for(i=0; i<checkbox.length; i++) {
if(checkbox[i].checked) {
return true;
}
}
}
alert("You must choose at least one record.");
return false;
}

What I can''t understand here is why that function works only when there is a
single ''='' here:
if(checkbox.length = "undefined")... ???
Checking for null produces Javascript errors or does nothing.

And how can I make it work if there are 0 rows [and thus 0 instances of
checkbox v1 ?]

Thank you in advance,
Oleg.

推荐答案

Oleg Konovalov写道:

....
Oleg Konovalov wrote:
....

I我正在开发一个Web应用程序,其中包括使用DHTML,Java

和Javascript。
I am working on a web application which among other things uses DHTML, Java
and Javascript.



调试JavaScript和HTML时,Java程序员

是最难问的人。试试..

comp.lang.javascript


Andrew T.

When debugging JavaScript amd HTML, Java programmers
are the worst people to ask. Try..
comp.lang.javascript

Andrew T.


我正在开发一个Web应用程序,其中包括使用DHTML,Java
I am working on a web application which among other things uses DHTML, Java

和Javascript。

它根据内容填充网页数据库(结果集),

和每行旁边有一个复选框(v1)允许选择该行

进行更改(例如删除,更新等) )

所以我们正在创建一个复选框数组,对吗?
and Javascript.
It populates web page based on the contents of the database (resultset),
and next to each row there is a checkbox (v1) allowing to select that row
for changes (e.g. delete, update, etc.)
So we are creating an array of checkbox, correct ?



" collection"更适合作为阵列暗示一个特定的

编程实体,这里没有介绍。


< snip>

"collection" is more appropriate as "array" implies a particular
programming entity which is not presented here.

<snip>


现在变得很奇怪。

我注意到如果有一行[即... 1复选框v1],该功能

始终显示警告并返回false:

function isAnySelected(checkbox)//检查是否至少有一个复选框是

选择

{

for(i = 0; i< checkbox.length; i ++){

if(checkbox [i]。已检查){

返回true;

}

alert("您必须至少选择一条记录。);

返回false;

}

为什么?
Now it becomes strange.
I have noticed that if there is 1 row [i.e. 1 checkbox v1], that function
always shows warning and returns false:
function isAnySelected(checkbox) //check if at least one checkbox is
selected
{
for(i=0; i<checkbox.length; i++) {
if(checkbox[i].checked) {
return true;
}
alert("You must choose at least one record.");
return false;
}
Why ?



因为你在这里处理HTML表单的DOM 0模型和

这个模型对命名表单元素的内部调用是多态:

如果只有一个元素具有给定形式的给定名称,则返回对此元素的

引用。默认情况下,HTML复选框

没有长度属性因此它被正确报告为未定义。

如果给定形式中有多个具有给定名称的元素,则

a使用此名称的所有元素的集合是创建并返回此集合的引用

。分别是长度和长度。物业

设置为此集合的长度。

为了使您的代码正确,您必须准备接受

元素参考和集合参考:


函数isAnyChecked(check){

var isChecked = false;


if ((typeof check.length ==''undefined)&&(check.checked)){

isChecked = true;

}


else {

for(var i = 0; i< check.length; i ++){

if(check [i] .checked){

isChecked = true;

休息;

}

}


return isChecked;

}

Because you are dealing here with the DOM 0 model for HTML forms and in
this model the internal call for a named form element is polymorphic:
If there is only one element with given name in given form, then a
reference to this element is returned. And HTML checkbox by default
doesn''t have "length" property so it''s properly reported as undefined.
If there are more than one element with given name in given form, then
a collection of all elements with this name is created and a reference
to this collection is returned. Respectively you have "length" property
set to the length of this collection.
To make your code correct, you have to make it ready to accept both an
element reference and a collection reference:

function isAnyChecked(check) {
var isChecked = false;

if ((typeof check.length == ''undefined)&&(check.checked)) {
isChecked = true;
}

else {
for(var i=0; i<check.length; i++) {
if (check[i].checked) {
isChecked = true;
break;
}
}

return isChecked;
}


if(checkbox.length =" undefined"){// with single ''=''!!!
if(checkbox.length = "undefined") { // with single ''='' !!!



这是一个花哨的构造,它表明你需要多么小心

条件检查JavaScript并建议学习这门语言

更好:-)


所以你得到(如前所述)表格复选框参考所以

它没有''' t具有长度。属性。但是在JavaScript中,如果您对一个不存在的属性进行

分配,它只需创建具有给定名称和赋值的新属性



所以你刚刚创建了新属性length字符串值

" undefined" ;.

赋值语句有自己的返回值:这是

赋值结果的值。所以checkbox.length =" undefined"声明返回

string" undefined"这是if()块得到的价值。但在

JavaScript中,布尔检查中的任何非空字符串都被视为真。

如此有效

if(checkbox.length =" undefined")

等于

if(true)

所以相关分支总是被执行。但是如果表单中有多个

复选框,则会产生运行时错误,因为你不是允许为收集长度赋值的
,它是'只读。

That''s a fancy construct, it shows how careful you have to be with
conditions check in JavaScript and suggests to learn this language
better :-)

So you are getting (as explained earlier) a form checkbox reference so
it doesn''t have "length" property. But in JavaScript if you make an
assignment to a non-existing property, it simply creates new property
with the given name and with the assignment value.
So you just created new property "length" with string value
"undefined".
Assignment statements have their own return value: this is value of the
assignment result. So checkbox.length = "undefined" statement returns
string "undefined" and this is value thant if() block gets. But in
JavaScript any non-empty string in boolean checks is treated as true.
So effectively
if (checkbox.length = "undefined")
is equal to
if (true)
so the relevant branch is always executed. But in case of more than one
checkbox in the form is will produce run-time error because you are not
allowed to assign values to collection length, it''s read-only.


为了使你的代码正确,你必须准备接受
To make your code correct, you have to make it ready to accept both an

元素参考和集合参考:


函数isAnyChecked(check){

var isChecked = false;

if((typeof check.length ==''undefined)&&(check.checked)){

isChecked = true;

}


else {

for(var i = 0; i< check.length; i ++){

if(check [check i] .checked){

isChecked = true;

休息;

}

}


返回isChecked;

}
element reference and a collection reference:

function isAnyChecked(check) {
var isChecked = false;

if ((typeof check.length == ''undefined)&&(check.checked)) {
isChecked = true;
}

else {
for(var i=0; i<check.length; i++) {
if (check[i].checked) {
isChecked = true;
break;
}
}

return isChecked;
}



这就是保持DOM 0接近。您还可以使用

document.getElementsByName(strName)方法保证

在任何情况下都返回一个集合,即使是单个元素也是如此

名字。缺点是这是一种文档方法,而不是表单。如果

你在一个页面上有多个表格,并且统一命名的复选框,

你最终将获得所有表格中的所有复选框

绝对不是你想要的。恕我直言0的方法更加强大,便携式可以用于支付。

That is if keeping DOM 0 approach. You can also use
document.getElementsByName(strName) method which is guaranteed to
return a collection in any case, even for a single element with such
name. The drawback is that this is a method of document, not a form. If
you have several forms on one page with uniformly named checkbox sets,
you''ll end up by getting all checkboxes from all forms which is
definitely not what you want. IMHO DOM 0 approach is more robust and
portable.


这篇关于在Javascript中未定义且为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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