Possible Duplicate:
Javascript create variable from its name
The code below checks to see if the javascript object form_errors has the property whose name is specified by this.name, where this refers to a text input
if (form_errors.hasOwnProperty(this.name)) {
alert(form_errors.<this.name>;
}
How can I access the property without hard-coding the property name but leaving in the generalized form this.name ? Tha开发者_C百科nks.
Use brackets:
form_errors[this.name]
You can access any property of an object by passing in a string with its name. For instance, foo.bar
and foo['bar']
have the same result.
精彩评论