You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Timo Thomas <t....@geodok.de> on 2006/03/23 11:29:29 UTC
Re: Using Javascript to submit a form doesn't work [Problem Solved]
Hi, this thread is a little old, but I maybe someone stumbles on it like
I did some days ago.
The proposed solutions still don't allow the event listeners registered
on the form's submit event to be called (e.g. using
form.addEventListener()). Calling form.onsubmit() eplicitely doesn't
help. A button with type="submit" has to be clicked, I found no other
way (alternatives welcome!).
Thus, I recommend the following:
1. do everything you need when the form is submitted in the onsubmit
event handler of the form
2. create a dummy, invisible submit button:
<h:commandButton id="dummySubmit" type="submit" style="display: none"/>
3. using the following code to programmtically submit the form:
function submitForm() {
document.getElementById('myForm:dummySubmit').click();
}
Note: this solution was tested with IE 6.0 and FF 1.5 only.
Timo
Paul Klaer schrieb:
> Here is a better example to show the difference between input fields of
> type "submit" and js script... Normally you would expect on both buttons
> the same effect...
>
> <html>
> <script>
> function doSubmit() {
> if(document.forms['theForm'].onsubmit()) {
> submit();
> }
> }
> </script>
> <body>
> <form id="theForm" onsubmit="alert('form
> onsubmit');return true;">
> <input type="submit" value="submit"
> onclick="doSubmit()" />
> <input type="button" value="button test js submit"
> onclick="doSubmit()">
> </form>
> </body>
> </html>
>
> On Wed, 28 Sep 2005 23:46:51 +0200, Paul Klaer <pk...@infeurope.lu> wrote:
>
>> Oh, sorry. You're right.
>>
>> Didn't saw that this script is invoked by a js command submit(). But
>> if you click on the browser "submit" button you need to be careful,
>> because the browser executes onsubmit:
>>
>> <html>
>> <body>
>> <form id="theForm" onsubmit="alert('form onsubmit');return
>> true;">
>> <input type="submit" value="submit" />
>> <input type="button"
>> onclick="document.forms['theForm'].submit();" value="button test js
>> submit">
>> </form>
>> </body>
>> </html>
>>
>> On Wed, 28 Sep 2005 23:21:09 +0200, Matt Blum <mb...@gmail.com> wrote:
>>
>>> No, it's not. When you invoke the submit method on a form, its onsubmit
>>> handler is not executed at all. That's the reason this code is
>>> necessary in
>>> the first place.
>>>
>>> -Matt
>>>
>>> On 9/28/05, Paul Klaer <pk...@infeurope.lu> wrote:
>>>>
>>>> Be careful with that script!
>>>>
>>>> Your onsubmit method is executed twice if it will return a value true!
>>>>
>>>> You have to use this code:
>>>>
>>>> if(document.forms['body:theForm'].onsubmit) {
>>>> document.forms['body:theForm'].submit();
>>>> } else {
>>>> document.forms['body:theForm'].submit();
>>>> }
>>>>
>>>> otherwise:
>>>>
>>>> if(document.forms['body:theForm'].onsubmit) {
>>>> if(document.forms['body:theForm'].onsubmit()) <<<<<<<- onsubmit is
>>>> executed here and if it returns true submit
>>>> document.forms['body:theForm'].submit(); <<<<<<<-- and onsubmit is
>>>> executed here
>>>> } else {
>>>> document.forms['body:theForm'].submit();
>>>> }
>>>>
>>>> On Wed, 28 Sep 2005 03:24:06 +0200, Saul Qunming Yuan
>>>> <yu...@dataanvil.com>
>>>> wrote:
>>>>
>>>> > if(document.forms['body:theForm'].onsubmit) {
>>>> > if(document.forms['body:theForm'].onsubmit())
>>>> > document.forms['body:theForm'].submit();
>>>> > } else {
>>>> > document.forms['body:theForm'].submit();
>>>> > }
>>>>
>>>>
>>>>
>>
>>
>
>
>
>