如何使用Jquery在页面加载时触发表单提交

时间:2011-12-14 19:16:44

标签: jquery

这似乎不太难实现,但我很匆忙,我一直在寻找一个简单的答案。我需要在页面加载时提交表单。这是我的AJAX提交功能,工作正常。我只需要弄清楚如何在页面加载时触发它。

非常感谢任何帮助!

$("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;  
    });

7 个答案:

答案 0 :(得分:8)

如果你致电$().submit(),它会触发行动; $().submit(function)将处理程序绑定到submit事件。

您可以跳过提交,然后直接调用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();});  

      }  
  });
});

答案 1 :(得分:3)

要在页面加载时实际提交表单,您可以编写类似

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

但是,如果你要做的只是执行与提交表单时原本相同的操作,那么也许你不想提交表单,只是为了这样做: / p>

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();
});

答案 2 :(得分: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');

});

您可以使用.trigger()功能触发表单上的submit事件。我链接了调用,因此只需要选择一次表单。注意,您要确保在触发submit事件之前设置submit事件处理程序。

文档:http://api.jquery.com/trigger

答案 3 :(得分:0)

在表单上调用$("#seller-form").submit(),这将触发提交事件。

答案 4 :(得分:0)

为什么不

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 :(得分:0)

使用jquery trigger()函数触发document.ready事件中的提交事件

答案 6 :(得分:0)

不确定这是否是你要求的

$(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;  
    });
});
相关问题