天天看点

Form表单转化成Json格式

$.fn.serializeObject = function(){

    var o={};

    var a = this.serializeArray();

    $.each(a,function(){

        if(o[this.name]){

            if(!o[this.name].push){

                o[this.name]=[o[this.name]];

            }

            o[this.name].push(this.value || '');

        }else{

            o[this.name]=this.value || '';

        }

    });

    return o;

};

界面引用:

Form表单转化成Json格式

表单:

Form表单转化成Json格式

效果监测:

<script type="text/javascript">

    $(function(){

        $("#reg").click(function(){

            var json = $('#form1').serializeObject();

            alert(JSON.stringify(json));

        });

    });

    </script>

Form表单转化成Json格式

// ajax 的调用:

<script type="text/javascript">

    $(function(){

        $("#reg").click(function(){

            var json = $('#form1').serializeObject();

            //是否为Json格式

            //alert(JSON.stringify(json));

            $.ajax({

                url:"<%=basePath%>/registQQ",

                type:"post",

                dataType:"json",

                data:json,

                success:function(result){

                    alert("成功了!");

                }

            });

        });

    });

    </script>