jQuery Ajax 确定 form 表单 submit 提交成功

使用 jQuery 提交表单,可以使用 同步方式(async: false)。

a.html 是 html 文件,a.php 是服务端文件,把 a.html 中表单的数据提交到 a.php 中,在提交成功之后,给出成功提示。

a.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="jquery-1.8.3.min.js"></script>
</head>
<body>
<form id="myform" action="a.php" method="post">
<input type="text" id="name" name="name">
<input type="hidden" name="myform[]" value="aaa">
<input type="hidden" name="myform[]" value="bbb">
<input type="hidden" name="myform[]" value="ccc">
<input type="button" id="btn" value="提交">
</form>
</body>
<script>
$(function(){
    $("#btn").click(function(){
        var postData = $("#myform").serialize();
        $.ajax({
             url: "a.php",
             //同步方式
             async: false,
             data: postData,
             type: "POST",
        //指定请求成功后执行的回调函数 success:
function(){ alert("success"); }, error: function(){ alert("error"); } }); }) }) </script> </html>

a.php:

<?php
print_r($_POST);

提交之后,在 chrome 开发者工具中查看:

 

参考:http://www.365mini.com/page/jquery_ajax.htm

http://doc.okbase.net/moqiang02/archive/93963.html

posted @ 2015-01-20 00:33  nemo20  阅读(3042)  评论(0编辑  收藏  举报
访客数:AmazingCounters.com
2016/05/17 起统计