Страница 1 из 1

Колличество элементов в форме

Добавлено: 13 июл 2004, 11:25
Oscar
Форма с 6-ю элементами:

Код: Выделить всё

<form onSubmit="return check(this)">
 ....

Код: Выделить всё

<script>
function check(fform)
{
 //строка0
 qSize = fform.elements.length;
 document.write("qSize = "+qSize+"
");   //строка1
 document.write("length = "+fform.elements.length+"
"); //стока2 
}
</script>
Вывод:
qSize = 6
length = 0

Если поменять местами строка1 и строка2, оно выводит:
length = 6
qSize = 6

А если строка2 поставить на место строка0, результат будет:
length = 6
qSize = 0

Внимание, вопрос:
Это я просто недоспал, или оно обнуляет elements.length в каких-то случаях?
Ну и собстно в каких?..

Добавлено: 13 июл 2004, 11:38
Oscar
А так ещё интереснее:

----begin: test.htm----

<script>
function check(fform)
{
document.write("length = " + fform.elements.length + "
");
document.write("length = " + fform.elements.length + "
");
}
</script>

<form onSubmit="check(this)">
<input type="Submit" Value="Senden">
</form>

----end: test.htm----

Вывод:
length = 1
length = 0

Добавлено: 13 июл 2004, 12:17
AiK
Oscar, всё разумно. Ты document.write на alert поменяй...

Добавлено: 13 июл 2004, 12:31
Oscar
AiK, с alert работает, я догадывался, что document.write может быть проблемой.
Но не могу понять, почему?
Можешь обьяснить в двух словах? Или послать куда-нить? :-) почитать в смысле ))

Добавлено: 13 июл 2004, 12:37
AiK
MSDN: You should not use the write or writeln methods on the current document after the document has finished loading unless you first call the open method, which clears the current document's window and erases all variables.

Добавлено: 13 июл 2004, 13:01
Oscar
AiK, thanks