Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

jquery - 使用jQuery提交表单(Submit a form using jQuery)

I want to submit a form using jQuery.

(我想使用jQuery提交表单。)

Can someone provide the code, a demo or an example link?

(有人可以提供代码,演示或示例链接吗?)

  ask by mehul translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It depends on whether you are submitting the form normally or via an AJAX call.

(这取决于您是正常提交??表单还是通过AJAX调用提交表单。)

You can find lots of information at jquery.com , including documentation with examples.

(您可以在jquery.com上找到很多信息,包括带有示例的文档。)

For submitting a form normally, check out the submit() method to at that site.

(要正常提交表单,请在该站点签出submit()方法。)

For AJAX , there are many different possibilities, though you probably want to use either the ajax() or post() methods.

(对于AJAX ,有许多不同的可能性,尽管您可能想使用ajax()post()方法。)

Note that post() is really just a convenient way to call the ajax() method with a simplified, and limited, interface.

(注意, post()实际上只是使用简化且受限制的接口调用ajax()方法的便捷方法。)

A critical resource, one I use every day, that you should bookmark is How jQuery Works .

(我每天都要使用的一项重要资源,即jQuery How Works ,应该用作书签。)

It has tutorials on using jQuery and the left-hand navigation gives access to all of the documentation.

(它具有使用jQuery的教程,左侧导航提供了访问所有文档的权限。)

Examples:

(例子:)

Normal

(正常)

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

AJAX

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

Note that the ajax() and post() methods above are equivalent.

(请注意,上面的ajax()post()方法是等效的。)

There are additional parameters you can add to the ajax() request to handle errors, etc.

(您可以将其他参数添加到ajax()请求中以处理错误等。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...