bodyonload
は正常に動作します。コードに他の問題がある可能性があります。aForms[0] はフォームへの参照であり、すべての要素を含む配列ではありません。
使用する必要があります
aForms[0] = document.getElementById('form1')
var elements =aForms[0].getElementsByTagName('input');
// Now elements will contain all the input elements inside that aForm[0] i.e form1
for (i=0; i<elements.length; i++){
//some codes...
}
document.forms
これは、ほとんどすべてのブラウザーでサポートされているフォームのコレクションを返します。ここに例があります
<body>
<form></form>
<form></form>
</body>
window.onload =function()
{
var Forms = document.forms;
for(var i=0;i<Forms.length;i++)
{
// Form[0] will contain reference to first form
// You can use Form[i] to refer to respective form
// If you want to check for id then following code may help you
// Even you can check for name to by .name property
if(Forms[0].id =='yourid')
{
// this is the form i want
}
}
}
フォームが iframe 内に含まれていて、同じドメインにある場合
var Frames = document.getElementById( 'yourIframeId' );
var DOC = Frames.contentDocument || Frames.contentWindow.document;
var aForm = DOC.getElementById( 'form1');