AJAX 提交表单实例

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> 
</head>
<body>
  <form action="t1.php" method="post" name="xx">
    用户名: <input type="text" id="name" name="name" value="zhangsan" /><br />
    密  码: <input type="text" id="pwd" name="pwd" value="123456" /><br />
    性  别: <input type="text" id="sex" name="sex" value="0" /><br />
    <input type="submit" name="" value="提交">
  </form>
  <div></div>
  <script type="text/javascript">
    $('form[name="xx"]').submit(function(){
         var form = $(this);
         // alert(form.attr('method'));
         $.ajax({
             type: form.attr('method'),
             url: form.attr('action'),
             data: form.serialize(),
             dataType: "json",
             beforeSend: function(){
             },
             complete:function(){
             },
             success: function(res){
                
             }
         });
        return false;
     });
  </script>
</body>
</html>

猜你喜欢

转载自my.oschina.net/u/2494575/blog/1808132