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

查看:27
本文介绍了使用 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,有许多不同的可能性,尽管您可能想使用 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天全站免登陆