如何使用 Jquery 在页面加载时触发表单提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8510343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I trigger a form submission on page load using Jquery
提问by Vaughn D. Taylor
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.
这似乎不太难完成,但我很匆忙,一直在寻找一个简单的答案。我需要在页面加载时提交表单。这是我的 AJAX 提交功能,工作正常。我只需要弄清楚如何在页面加载时触发它。
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;
});
回答by Mathletics
if you call $().submit()
it triggers the action; $().submit(function)
binds a handler to the submit event.
如果你调用$().submit()
它会触发动作;$().submit(function)
将处理程序绑定到提交事件。
You can skip the submit however and just call the ajax method directly.
但是,您可以跳过提交并直接调用 ajax 方法。
$(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();});
}
});
});
回答by dxh
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();
});
回答by Jasper
$(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.
您可以使用该.trigger()
函数来触发submit
表单上的事件。我将调用链接起来,因此表单只需选择一次。请注意,您要确保submit
在触发submit
事件之前设置事件处理程序。
Documentation: http://api.jquery.com/trigger
回答by kvc
回答by kvc
Call $("#seller-form").submit()
on the form and this will trigger the submit event.
调用$("#seller-form").submit()
表单,这将触发提交事件。
回答by JesseBuesking
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);
回答by Andres
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;
});
});