ajax提交表单的几种形式

https://blog.csdn.net/qq_17164811/article/details/78203766

1.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
    <script type="text/javascript">
        $(function(){

            $.ajax({
                url:'xxxx',
                type:'post',
                dataType:'json',
                data:{
                    'p1':$('#p1'),
                    'p2':$('#p2')
                },
                success:function(result){
                    //回调函数
                }

            });


        });
    </script>
</head>
<body>
    <form id="myForm" action="">
        <input id="p1" name="p1" value="p1"/>
        <input id="p2" name="p2" value="p2"/>
        <input id="btn" type="button" value="提交"/>
    </form>
</body>
</html>

2.$('#id').serialize( ); 这时表单的按钮的type不可以是submit,否则自提交数据,也就是自动刷新;改为button
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
    <script type="text/javascript">
        $(function(){

            $.ajax({
                url:'xxxx',
                type:'post',
                dataType:'json',
                data:$("#myForm").serialize(),
                success:function(result){
                    //回调函数
                }
            });


        });
    </script>
</head>
<body>
    <form id="myForm" action="">
        <input id="p1" name="p1" value="p1"/>
        <input id="p2" name="p2" value="p2"/>
        <input id="btn" type="button" value="提交"/>
    </form>
</body>
</html>

3.使用jQuery Form插件提供的ajaxSubmit()函数
$('#myform').ajaxSubmit({
type: 'GET/POST',
url: 'xx.php',
dataType: 'json',
success: fn,
clearForm: true,
resetForm: true
}); 

 

posted @ 2018-04-09 16:42  aLa神灯  阅读(216)  评论(0编辑  收藏  举报