Home  >  Article  >  Web Front-end  >  jquery 使用ajax提交form表单

jquery 使用ajax提交form表单

WBOY
WBOYOriginal
2016-06-01 09:54:56914browse

我们可以使用jquery的serialize()方法来获取表单的数据,然后使用ajax提交表单。 具体代码如下:

<code><form id="contactForm1" action="/your_url.php" method="post">
    <input type="text" name="category" value="123"><br>
    <input type="text" name="school" value="youschool"><br>
</form>
<script type="text/javascript" src="/Public/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
    var frm = $('#contactForm1');
    frm.submit(function (ev) {
        $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
                alert('ok');
            }
        });

        ev.preventDefault();
    });
</script></code>

 

Statement:
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