问题描述
伙计,
我写了一些脚本工具,可以与我的
页面相容 - 例如,我已经创建了一个脚本,它将检查确保
某些需要数据的表单字段已完成。为了使这个
起作用,我必须传递它的表格名称和字段名称(输入标签
名称)。
有时我将语法从一个页面复制/粘贴到另一个页面,我可能会忘记更改表单名称,或者在
函数中传递的输入标记名称调用结果我在Mozilla js
控制台中收到一条关于没有属性的对象的消息。
我可以让我的脚本测试吗?我可以测试,例如,
document.forms [formName]是否存在,其中formName是一个变量?如果是这样的话,
对我来说测试的最佳方法是什么?
谢谢...通过ng回复所有人都可以学习..非常感谢,
randelld
Folks,
I have written some scripting tools that are compatable with alot of my
pages - For example, I''ve created a script that will check to ensure
certain form fields that require data, are completed. In order for this
to work, I must pass it the form name, and the field names (input tag
names).
Sometimes I copy/paste the syntax from one page to another and I might
forget to change the form name, or an input tag name passed in the
function call with the result that I get a message in the Mozilla js
console about an object with no properties.
Can I have my scripts test for this? Can I test, for example, if
document.forms[formName] exists, where formName is a variable? If so,
what would be the best method for me to test for this?
Thanks... replies via ng so that all can learn... is greatly appreciated,
randelld
推荐答案
if(typeof document.forms [formName]!=" undefined" ;){
/ *做点什么* /
}否则{
/ *未定义* /
}
-
ZER0
~Tangent Universe倒塌5965天,5小时2分钟13秒前。
if (typeof document.forms[formName]!="undefined"){
/* do something */
}else{
/* is undefined */
}
--
ZER0
~ The Tangent Universe collapsed 5965 days, 5 hours, 2 minutes and 13 seconds ago.
虽然
如果(document.forms [formName]){
就足够了。
Mike
-
Michael Winter
替换.invalid与.uk通过电子邮件回复。
Though
if(document.forms[formName]) {
should be sufficient.
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
if(typeof document.forms [formName]!=" undefined"){
/ *做点什么* /
}否则{
/ *未定义* /
}
if (typeof document.forms[formName]!="undefined"){
/* do something */
}else{
/* is undefined */
}
只是为了展开关于Mike的回复,上面的类型测试是否需要
如果您正在测试一个可能返回零的函数
,因此即使它受到支持,它也是如此将无法通过
测试。
例如if(document.body.scrollLeft){
...
}
如果文件没有,则返回零实际上滚动了
到右边,测试将失败。
即使你想在表格上使用这种类型的测试
集合,我想:
if(document.forms [formName]!=''undefined''){
就足够了。
-
Rob
Just to expand a little on Mike''s reply, the above type test is
required if you are testing a function that may return zero
and therefore even though it is supported, it will fail the
test.
e.g. if (document.body.scrollLeft) {
...
}
Will return zero if the document hasn''t actually been scrolled
to the right and the test will fail.
Even if you wanted to use this type of test on the forms
collection, I think:
if ( document.forms[formName] != ''undefined''){
would be sufficient.
--
Rob
这篇关于我可以检查对象是否存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!