How do I trigger a form submission on page load using Jquery

空扰寡人 提交于 2021-02-17 22:54:27

问题


This doesn't seem like it should be too difficult to accomplish, but I'm in a rush and I have been looking for a simple answer. I need to submit a form on page load. This is my AJAX submit function which works fine. I just need to figure out how to trigger it on page load.

Any help is much appreciated!

$("form#seller-agreement-form").submit(function() {  
        // we want to store the values from the form input box, then send via ajax below    
        $.ajax({  
            type: "POST",  
            url: "http://www2.myurl.com/formhandler",  
            data: "email="+ email + "&status=" + status,  
            success: function(){  
                $('form#seller-agreement-form').hide(function(){$('div#output').fadeIn();});  

            }  
        });  
    return false;  
    });

回答1:


if you call $().submit() it triggers the action; $().submit(function) binds a handler to the submit event.

You can skip the submit however and just call the ajax method directly.

$(function() {
  $.ajax({  
      type: "POST",  
      url: "http://www2.myurl.com/formhandler",  
      data: "email="+ email + "&status=" + status,  
      success: function(){  
          $('form#seller-agreement-form').hide(function(){$('div#output').fadeIn();});  

      }  
  });
});



回答2:


To actually submit the form on page load, you would write something like

$(function() {
   $('#seller-agreement-form').submit();
});

But if what you're trying to do is just to perform the same action that you would otherwise have performed when the form was submitted, then perhaps you don't want to submit the form, but only to do this:

function postForm() {
    $.ajax({  
        type: "POST",  
        url: "http://www2.myurl.com/formhandler",  
        data: "email="+ email + "&status=" + status,  
        success: function(){  
            $('form#seller-agreement-form').hide(function(){$('div#output').fadeIn();});  

        }  
    }); 
}

$("form#seller-agreement-form").submit(function() {
    postForm();
    return false;
});

$(function() {
    postForm();
});



回答3:


$(function () {
    $("form#seller-agreement-form").submit(function() {  
        // we want to store the values from the form input box, then send via ajax below    
        $.ajax({  
            type: "POST",  
            url: "http://www2.myurl.com/formhandler",  
            data: "email="+ email + "&status=" + status,  
            success: function(){  
                $('form#seller-agreement-form').hide(function(){$('div#output').fadeIn();});  

            }  
        });  
    return false;  
    }).trigger('submit');

});

You can use the .trigger() function to trigger the submit event on the form. I chained the calls so the form only has to be selected once. Note, you want to make sure to set the submit event handler before triggering the submit event.

Documentation: http://api.jquery.com/trigger




回答4:


Why not do

function AjaxCall() {
    // we want to store the values from the form input box, then send via ajax below    
    $.ajax({
        type: "POST",
        url: "http://www2.myurl.com/formhandler",
        data: "email=" + email + "&status=" + status,
        success: function() {
            $('form#seller-agreement-form').hide(function() {
                $('div#output').fadeIn();
            });

        }
    });
    return false;
}

AjaxCall();

$("form#seller-agreement-form").submit(AjaxCall);



回答5:


Use jquery trigger() function to trigger submit event in your document.ready event




回答6:


Call $("#seller-form").submit() on the form and this will trigger the submit event.




回答7:


not sure if this is what you're asking for

$(document).ready(function () {
    $("form#seller-agreement-form").submit(function() {  
        // we want to store the values from the form input box, then send via ajax below    
        $.ajax({  
            type: "POST",  
            url: "http://www2.myurl.com/formhandler",  
            data: "email="+ email + "&status=" + status,  
            success: function(){  
                $('form#seller-agreement-form').hide(function(){$('div#output').fadeIn();});  

            }  
        });  
    return false;  
    });
});


来源:https://stackoverflow.com/questions/8510343/how-do-i-trigger-a-form-submission-on-page-load-using-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!