简体   繁体   中英

How to submit a form on page load without using jquery in AngularJS?

I have a form in AngularJS application. I need to auto submit this form upon page load without using jquery because of cross domain issue.

When I submit the form by using a submit button it works and the target url loads in the browser. If I remove the submit button and try to submit in the onload event, it does not work. The page displays just ";". Any idea why onload does not work here? Thank you!

<form name="myForm" method="post" action="@Model.Settings["URL"]" ng-controller="PostCtrl">
    <input type="hidden" name="Name" value="{{Details.Name}}">
    <input type="hidden" name="Amount" value="{{Details.Amount}}">

     @*<button type="submit" class="action blue"><span class="label">Click here</span></button>*@

    <script>
        window.onload = function () {
            document.myForm.submit();
        }
    </script>
</form>

I have some suggestions for you. But first you have to add an id to your form: 1: Go to your <html> element and call the onload function there --> <html onload="document.getElementById('yourform').submit();"> or 2: call the JavaScript function with php

<?php
echo "<script>document.getElementById('yourform').submit();</script>";
?>

Or 3: try to change your Script to

<script>
window.onload = function () {
            document.getElementById('yourform').submit();
        }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM