質問

I have a form which I want to submit on bodyLoad.

I have written document.forms[0].submit() which works fine with IE 9 and Chrome 14 but same is not working in FF 3.6.23.

Any ideas? Is this a known issue with FF?

I have tried other options like document.form_name.submit() and document.getElementById('form_id').submit() but nothing works with FF.

The error I am getting in FF is

document.forms[0] is undefined

This is what I have written in the view (CakePHP 1.2.6):

<?php $this->layout = 'blank'; ?>

<?php e($form->create('Mymodel', array('name'=>'myform', 'url'=>'gohere'))); ?>

<?php e($form->hidden('name', array('value'=>$name))); ?>

<?php e($form->end()); ?>

<script language="javascript">
document.forms[0].submit();
</script>
役に立ちましたか?

解決

That won't work in Firefox, since the page isn't fully loaded yet.

Eliminate

document.forms[0].submit();

and set

<body onLoad="document.forms[0].submit();">

Be careful not to point <form> to the current page, as it would cause an endless loop.

他のヒント

The javascript code may be executed before the form is actually loaded. That's why Firefox can't find document.forms[0]. You have to make sure your page is ready before executing javascript. With JQuery, you do it with $(document).ready()

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top