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;
};

界面引用:

表单:

效果监测:

<script type="text/javascript">
    $(function(){
        $("#reg").click(function(){
            var json = $('#form1').serializeObject();
            alert(JSON.stringify(json));
        });
    });
    </script>

// 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>

猜你喜欢

转载自blog.csdn.net/word_joke/article/details/83338198