JQuery ajax api表单提交

来源:互联网 发布:千牛软件 编辑:程序博客网 时间:2024/05/18 20:04
jquery ajax api,表单提交方法一:

// this is the id of the submit button
$("#submitButtonId").click(function() {

    var url = "path/to/your/script.php"; // the script where you handle the form input.

    $.ajax({
           type: "POST",
           url: url,
           data: $("#idForm").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });

    return false; // avoid to execute the actual submit of the form.
});


表单提交方法二:

<form id="contactForm1" action="/your_url" method="post">
    ...
</form>

<script type="text/javascript">
    var frm = $('#contactForm1');
    frm.submit(function () {
        $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
                alert('ok');
            }
        });

        return false;
    });
</script>
原创粉丝点击