简体   繁体   中英

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"). However, I think the "input" bit is incorrect...

Can anyone help?

That code is looking for a property literally called "input" on frmRegister . To look for "fieldName" for example (the value of input ), you want bracketed notation:

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"] ). In the latter case, the property name string can be the result of any expression, including a variable or argument lookup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM