开发者

Submitting a form from inside a JavaScript function in Tapestry

开发者 https://www.devze.com 2023-01-29 10:29 出处:网络
I am trying to submit a form from inside a JavaScript function in Tapestry. Here is the tml file. <!DOCTYPE html>

I am trying to submit a form from inside a JavaScript function in Tapestry. Here is the tml file.

<!DOCTYPE html>
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd" xmlns:tx="tapestry-library:tapx">
<head>
    <script type="text/javascript">
        function bodyLoaded () {
          document.form1.submit();
        }
    </script>
</head>
<body onload="bodyLoaded()">
<form t:type="form" t:name="form1">
    <select t:type="select" t:id="reportType" t:model="literal:A, B"></select>
    <input t:type="submit" id="clie开发者_StackOverflowntSubmit" value="Generate"/>
</form>
</body>
</html>

But I am getting below error, and the form is not getting submitted.

document.form1 is undefined [Break on this error] document.form1.submit();

So I looked at the html code generated by Tapestry. It has following tag:

    <form onsubmit="javascript:Tapestry.waitForPage(event);" 
  action="test.form" method="post" id="form" name="form">

So I changed document.form1.submit() to document.form.submit(), but still it didn't solve the problem. Is there anything wrong with my code (or) doesn't hibernate allow to submit the form from inside JavaScript functions?


upgrade to tapestry 5.2. this was a known issue in prior 5.x versions.

https://issues.apache.org/jira/browse/TAP5-947


<form t:type="form" t:name="form1" t:id="form1">
    <select t:type="select" t:id="reportType" t:model="literal:A, B"></select>
    <input t:type="submit" id="clientSubmit" value="Generate"/>
</form>




<script type="text/javascript">
        function bodyLoaded () {
          document.getElementById('form1').submit();
        }
    </script>

i have just put id in the form tag and one change in java script.

This may help you.

Thanks.


instead of document.form try document.forms[0]


Use id of the form

<form t:type="form" t:name="form1" t:id="form1">

document.form1.submit();


I updated tapestry today from 5.0.1.5 to 5.0.1.8 to get rid of the annoying bug in AjaxFormLoop, and guess what I ran into :) After being frustrated for almost 4 hours I tried something that worked.

The error is caused by Tapestry automatically adding a waitForPage(event) javascript method on the form's onSubmit event (to prevent the form from being submitted before the page is loaded). When you say document.getElementById('form1').submit(); or any of its iterations suggested by the comments here, it seems to throw that exception because of a mismatch in the method arguments (I think). Anyway to fix it just change the line to document.getElementById('form1').submit(this);

This worked for me and I hope this solves your problem as well!

0

精彩评论

暂无评论...
验证码 换一张
取 消