自定义一个序列化表单的方法2+提示语

/** 自定义一个序列化表单的方法* */
    $.fn.serializeObject = function(a) {
    	var o = {};
        o.flag=false;
        var grounp=[];
    	if(a == 1){
    		$(this).find("input").each(function(index) {
	            if ($(this).attr("name") != undefined) {
	            	var _val = $(this).val().length;
		        	var must = $(this).data('must');
		        	var _tips = $(this).data('tip');
		        	var max = $(this).data('max');
	                if ($(this).attr("type") == "text" || $(this).attr("type") == "password" || $(this).attr("type") == "hidden") {
	                	$(this).siblings('.v_tips').remove();
	                	if(_val > max){ //超过有效长度
	                		$(this).after('<span class="v_tips" style="color:red;font-size:13px">'+_tips+'</span>');
	                		 o.flag=true;
	                	}
	                	if( must == 'true' && _val == 0){ //必填
	                		if(_tips != undefined){
	                			$(this).after('<span class="v_tips" style="color:red;font-size:13px">'+_tips+'</span');
	                			o.flag=true;
	                		}
	                	}else{
	                		o[$(this).attr("name")] = $(this).val();
	                	}
	                }
	                if ($(this).attr("type") == "radio") {
	                    if ($(this).is(":checked")) {
	                        o[$(this).attr("name")] = $(this).attr("value");
	                    }
	                }
	            }
	        });
    	}else if(a == 2){
    		var checkedIds = $(this).find("input[type='checkbox']");
    		var name = checkedIds[0].name;
    		var grounp = [];
	        if (checkedIds.length != 0) {
	            for (var i = 0; i < checkedIds.length; i++) {
	                if (checkedIds[i].checked == true) {
	                    grounp.push(checkedIds[i].value)
	                }
	            }
	        }
	        if(grounp.length == 0){
	        	o.flag=true;
	        }else{
	        	 o[name] = grounp;
	        }
    	}

        $(this).find("select").each(function(index) {
            if ($(this).attr("name") != undefined) {
                o[$(this).attr("name")] = $(this).val();
            }
        });

        $(this).find("textarea").each(function(index) {
            if ($(this).attr("name") != undefined) {
                $(this).siblings('.v_tips').remove();
                var _val = $(this).val().length;
                var must = $(this).data('must');
                var _tips = $(this).data('tip');
                var max = $(this).data('max');
                if(must == 'true'){
                    if(_val == 0 || _val > max){
                        $(this).after('<span class="v_tips" style="color:red;font-size:13px">'+_tips+'</span');
                        o.flag=true;
                    }else{
                        o[$(this).attr("name")] = $(this).val();
                    }
                }else{
                    if(_val > max){
                        $(this).after('<span class="v_tips" style="color:red;font-size:13px">'+_tips+'</span');
                        o.flag=true;
                    }else{
                        o[$(this).attr("name")] = $(this).val();
                    }
                }
            }
        });
        return o;
    };

    $(function(){
    	$(document).on('click','input[name="submit"]',function(){
    		var test = $('.textAndRadio').serializeObject(1); //text And  radio (type)
    		var checkbox = $('.checkboxRow').serializeObject(2); //checkbox
            var selectBox = $('.selectBox').serializeObject(); //select
            var textareaBox = $('.textareaBox').serializeObject(); //textarea
	    	if(test.flag || checkbox.flag || textareaBox.flag){ //为空时 checkbox.flag返回true
                console.log('不能为空');
	    		return;
	    	}else{
		    	delete test.flag; //删除父容器属性flag  (flag只做判断)
                var checked1 =checkbox.checked1; //获取checkbox中的checked1值
                test.checked1 = checked1; //test添加属性checked1 值为 checked1
		    	console.log(test);
                console.log(JSON.stringify(test));
	    	}
    	})

    })

html

<div class="rows textAndRadio">
        <input type="text" name="name1" value="22" data-must="false" data-tip="请输入正确文本" data-max="2" />
    </div>
    <div class="rows textAndRadio">
        <input type="text" name="name2" value="1" data-must="true" data-tip="请输入正确文本" data-max="2" />
    </div>
    <div class="rows textAndRadio">
        <input type="text" name="name3" value="" data-must="false" />
    </div>
    <div class="rows">
        <input type="radio" name="radio" value="1" checked="checked" data-must="true" data-tip="请输入正确文本" />
        <input type="radio" name="radio" value="0" data-must="true" data-tip="请输入正确文本" />
    </div>
    <div class="rows checkboxRow">
        <input type="checkbox" name="checked1" value="1" data-must="true" data-tip="请输入正确文本" />
        <input type="checkbox" name="checked1" value="0" data-must="true" data-tip="请输入正确文本" />
    </div>
    <div class="selectBox">
        <select name="select">
            <option value="1">11</option>
            <option value="2">11</option>
            <option value="3">11</option>
        </select>
    </div>
    <div class="textareaBox">
        <textarea name="textarea">13213</textarea>
    </div>
    <div class="textareaBox">
        <textarea name="textarea2" data-must="false" data-max="2" data-tip="请输入正确文本">2</textarea>
    </div>
    <input type="button" name="submit" value="提交">

 

转载于:https://my.oschina.net/newgoup/blog/753805

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值