[英]Passing a parameter into a Javascript method
I have the following code: 我有以下代码:
function isFieldEmpty(input)
{
if(document.frmRegister.input.value == "")
{
return false;
}
return true;
}
I call it using isFieldEmpty("fieldName"). 我用isFieldEmpty(“ fieldName”)称呼它。 However, I think the "input" bit is incorrect... 但是,我认为“输入”位不正确...
Can anyone help? 有人可以帮忙吗?
That code is looking for a property literally called "input"
on frmRegister
. 该代码正在frmRegister
上寻找一个字面意义为"input"
的frmRegister
。 To look for "fieldName"
for example (the value of input
), you want bracketed notation: 例如,要查找"fieldName"
( input
的值 ),需要使用方括号表示法:
if(document.frmRegister[input].value == "")
// Change -------------^-----^
In JavaScript, you can access the property of an object using either dot notation and a literal property name ( obj.foo
) or using bracketed notation and a string property name ( obj["foo"]
). 在JavaScript中,您可以使用点符号和文字属性名称( obj.foo
)或使用方括号和字符串属性名称( obj["foo"]
)访问obj["foo"]
属性。 In the latter case, the property name string can be the result of any expression, including a variable or argument lookup. 在后一种情况下,属性名称字符串可以是任何表达式的结果,包括变量或参数查找。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.