使用jQuery提交表单 [英] Submit a form using jQuery

查看:108
本文介绍了使用jQuery提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jQuery提交表单.有人可以提供代码,演示或示例链接吗?

I want to submit a form using jQuery. Can someone provide the code, a demo or an example link?

推荐答案

这取决于您是正常提交​​表单还是通过AJAX调用提交.您可以在 jquery.com 上找到很多信息,包括带有示例的文档.要正常提交表单,请在该站点上查看 submit() 方法.对于 AJAX ,虽然您可能要使用 post() 方法.请注意,post()确实是使用简化且受限制的接口调用ajax()方法的便捷方法.

It depends on whether you are submitting the form normally or via an AJAX call. You can find lots of information at jquery.com, including documentation with examples. For submitting a form normally, check out the submit() method to at that site. For AJAX, there are many different possibilities, though you probably want to use either the ajax() or post() methods. Note that post() is really just a convenient way to call the ajax() method with a simplified, and limited, interface.

我每天都使用的一项重要资源,应该用作书签,它是 jQuery的工作方式.它包含有关使用jQuery的教程,左侧导航提供了对所有文档的访问权限.

A critical resource, one I use every day, that you should bookmark is How jQuery Works. It has tutorials on using jQuery and the left-hand navigation gives access to all of the documentation.

示例:

正常

$('form#myForm').submit();

AJAX

$('input#submitButton').click( function() {
    $.post( 'some-url', $('form#myForm').serialize(), function(data) {
         // ... do something with response from server
       },
       'json' // I expect a JSON response
    );
});

$('input#submitButton').click( function() {
    $.ajax({
        url: 'some-url',
        type: 'post',
        dataType: 'json',
        data: $('form#myForm').serialize(),
        success: function(data) {
                   // ... do something with the data...
                 }
    });
});

请注意,上面的ajax()post()方法是等效的.您可以将其他参数添加到ajax()请求中以处理错误等.

Note that the ajax() and post() methods above are equivalent. There are additional parameters you can add to the ajax() request to handle errors, etc.

这篇关于使用jQuery提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆