Home > Web Front-end > JS Tutorial > Detailed explanation of how jQuery uses ajaxSubmit() to submit form instances

Detailed explanation of how jQuery uses ajaxSubmit() to submit form instances

巴扎黑
Release: 2017-07-03 10:47:43
Original
2264 people have browsed it

This article mainly introducesjQueryUsing ajaxSubmit() to submit a form example, using the extended third-party plug-in jquery.form to implement it. Friends who need it can refer to it

ajaxSubmit(obj) The method is a method in jquery.form.js, a plug-in of jQuery, so you need to introduce this plug-in first to use this method. As shown below:

The code is as follows:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://malsup.github.io/jquery.form.js"></script>
Copy after login

So, how to submit data through ajaxSubmit(obj)? First we need a form:

XHTML

The code is as follows:

<form>
    标题:<input type="text" name="title" /><br />
    内容:<textarea name="content"><textarea/><br />
    <button>提交</button>
</form>
Copy after login



The above is a form that needs to submit content, usually , if we submit directly through the form, the current page will jump to the page pointed to by action of the form after submission. However, many times we do not want the page to jump after submitting the form, then we can use ajaxSubmit(obj) to submit the data. The usage method is as follows:

The code is as follows:

$(&#39;button&#39;).on(&#39;click&#39;, function() {
    $(&#39;form&#39;).on(&#39;submit&#39;, function() {
        var title = $(&#39;inpur[name=title]&#39;).val(),
            content = $(&#39;textarea&#39;).val();
        $(this).ajaxSubmit({
            type: &#39;post&#39;, // 提交方式 get/post
            url: &#39;your url&#39;, // 需要提交的 url
            data: {
                &#39;title&#39;: title,
                &#39;content&#39;: content
            },
            success: function(data) { // data 保存提交后返回的数据,一般为 
json
 数据
                // 此处可对 data 作相关处理
                alert(&#39;提交成功!&#39;);
            }
            $(this).
reset
Form(); // 提交后重置表单
        });
        
return
 false; // 阻止表单自动提交
事件
    });
});
Copy after login


The above is the detailed content of Detailed explanation of how jQuery uses ajaxSubmit() to submit form instances. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template