简体   繁体   中英

Get form elements as associative array?

I have a problem with html form elements names as assotiative arrays.

<form name="ff" action="" method="POST">
<input name="student[john]" value="John">
<input name="student[kofi]">
<input name="student[kwame]">
<input type="submit" name="submit">
</form>
<a onclick="alert(document.ff.student[john].value);">a<a>

I'm getting JS error: "Uncaught exception: ReferenceError: Undefined variable: john"

But this code works fine with form elements names without square brackets.

I'm puzzled how to get values from radio elements (it represented as a group of radios with same names)

You'll have to use square bracket notation to access a property with special characters

<a onclick="alert(document.ff['student[john]'].value);">a<a>

DEMO

Element names are not arrays.

Try:

document.ff.elements['student[john]'].value

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