带有无效ID的getElementById导致脚本退出? [英] getElementById with invalid ID causes script to exit?

查看:76
本文介绍了带有无效ID的getElementById导致脚本退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个使用JavaScript来验证某些表格的网站

我似乎遇到了一些麻烦。该网站使用PHP和

之一的表单,根据情况,其中一个文本输入确实不存在
- PHP没有生成它的HTML。我想要验证脚本验证输入的长度,如果它存在,但是我知道如果我试图检查<的值,我可能会遇到麻烦br />
输入没有先检查输入是否存在所以我尝试使用

代码类似于:


if(document.getElementById( " someId")!= null&&

document.getElementById(" someId")。value.length< 8)

{

alert(" String必须至少8个字符。);

}


然而,当我在Firefox中尝试时(那个) '到目前为止我唯一尝试过的浏览器

),验证脚本退出,表单提交了

错误,我收到了一条消息JavaScript控制台说我给的

ID无效。我知道它无效!这就是为什么我检查

为空的原因!


我认为如果ID为
$ b $,getElementById()将返回null b无效。有没有办法在

验证其值之前检查输入是否存在?


-Brandon R.

解决方案

方法''document.getElementById(" someId")''返回一个Object的

引用。

so,''document.getElementById(" someId")。value.length< 8''应该是

modified''document.getElementById(" someId")。values.length< ; 8''


试试吧:)




bc ****** @ creighton.edu 写道:

if(document.getElementById(" someId")!= null&&
document.getElementById(" someId")。value.length< 8)
{
alert( 字符串必须至少为8个字符。;
}
然而,当我在Firefox中尝试时(那是我唯一的浏览器)到目前为止尝试了
,验证脚本退出,表单提交时出现
错误,我在JavaScript控制台中收到一条消息,说我给的
ID无效。 / blockquote>


请将您传递给getElementById的确切ID值发布并发布

确切的错误消息。这是哪个版本的Firefox?


-


Martin Honnen
http://JavaScript.FAQTs.com/


br ***** @ 163.com 在3/7/2006上午7:37发表以下内容:


请引用您要回复的内容。


如果您想通过groups.google.com发布后续内容,请不要使用

回复链接在文章的底部。点击显示选项在

文章的顶部,然后点击回复。在

文章标题的底部。 < URL: http://www.safalra.com/special/googlegroupsreply/ >

方法''document.getElementById(" someId")''返回一个Object的
引用。


仅当浏览器具有ID为someID的元素时才支持。

so,''document.getElementById(" someId")。 value.length< 8''应该被修改''document.getElementById(" someId")。values.length< 8''

试试吧:)




然后回来问为什么它仍然不起作用?没有

..values属性。


-

Randy

comp.lang .javascript常见问题解答 - http://jibbering.com/faq &新闻组每周

Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /


I am writing a web site that uses JavaScript to validate certain forms
and I seem to be having some trouble. The site uses PHP and for one of
the forms, depending on the situation, one of of the text inputs does
not exist - the PHP doesn''t generate the HTML for it. I wanted to have
the validation script check the length of the input if it exists but I
knew that I might have trouble if I tried to check the value of the
input without first checking that the input exists so I tried using
code similar to this:

if(document.getElementById("someId")!=null &&
document.getElementById("someId").value.length<8)
{
alert("String must be at least 8 characters.");
}

However, when I try that in Firefox (that''s the only browser I''ve tried
so far), the validation script exits, the form is submitted with
errors, and I get a message in the JavaScript console saying that the
ID that I gave was invalid. I know it''s invalid! That''s why I checked
for null!

I thought that getElementById() would return null if the ID was
invalid. Is there any way to check if the input exists before
validating its value?

-Brandon R.

解决方案

the method ''document.getElementById("someId")'' returns a Object''s
reference.
so, ''document.getElementById("someId").value.length<8'' should be
modified ''document.getElementById("someId").values.length<8 ''

just try it : )




bc******@creighton.edu wrote:

if(document.getElementById("someId")!=null &&
document.getElementById("someId").value.length<8)
{
alert("String must be at least 8 characters.");
}

However, when I try that in Firefox (that''s the only browser I''ve tried
so far), the validation script exits, the form is submitted with
errors, and I get a message in the JavaScript console saying that the
ID that I gave was invalid.



Please post the exact id value you pass to getElementById and post the
exact error message you get. Which version of Firefox is that?

--

Martin Honnen
http://JavaScript.FAQTs.com/


br*****@163.com said the following on 3/7/2006 7:37 AM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don''t use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers. <URL: http://www.safalra.com/special/googlegroupsreply/ >

the method ''document.getElementById("someId")'' returns a Object''s
reference.
Only if the browser has an element with an ID of "someID".
so, ''document.getElementById("someId").value.length<8'' should be
modified ''document.getElementById("someId").values.length<8 ''

just try it : )



And then come back and ask why it still doesn''t work? There is no
..values property.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


这篇关于带有无效ID的getElementById导致脚本退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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