JavaScript get element by name
Rozważmy tę funkcję:
function validate()
{
var acc = document.getElementsByName('acc').value;
var pass = document.getElementsByName('pass').value;
alert (acc);
}
I ta część HTML:
<table border="0" cellpadding="2" cellspacing="0" valign="top">
<tr>
<td class="td1">Account</td>
<td class="td2"><input type="text" name="acc" /></td>
</tr>
<tr class="td1">
<td>Password</td>
<td class="td2"><input type="password" name="pass" /></td>
</tr>
</table>
<div><button onClick="validate()" class="cupid-greenx">Login now</button></div>
Okno alertu jest wyświetlane, ale pokazuje "undefined".
109
Author: Brett DeWoody, 2012-04-25
5 answers
document.getElementsByName
zwraca NodeList elementów, więc nie ma właściwości .value
. stąd ten niezdefiniowany błąd, który widzisz.
What you want is document.getElementsByName("acc")[0].value
199
Author: Aidanc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-20 11:13:06
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-20 11:13:06
Zwróć uwagę na liczbę mnogą w tej metodzie:
document.getElementsByName()
, który zwraca tablicę elementów, więc użyj [0], aby uzyskać pierwsze wystąpienie, np.
document.getElementsByName()[0]
22
Author: Ozzy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-24 21:21:12
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-24 21:21:12
Chcesz tego:
function validate() {
var acc = document.getElementsByName('acc')[0].value;
var pass = document.getElementsByName('pass')[0].value;
alert (acc);
}
9
Author: Elliot Bonneville,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-24 21:20:23
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-24 21:20:23
document.getElementsByName("myInput")[0].value;
5
Author: Sam Battat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-24 21:20:47
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-24 21:20:47
Dokument metody.getElementsByName zwraca tablicę elementów. Na przykład powinieneś wybrać najpierw.
document.getElementsByName('acc')[0].value
5
Author: dalazx,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-24 21:21:38
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-24 21:21:38