前端非空验证js代码

<script>
    $(function(){
        var options = {
            beforeSubmit:  showRequest,  //提交前处理
            success:       showResponse1,  //处理完成
            resetForm: false,
            dataType:  'json'
        };

        $('#form1').submit(function() {
            $(this).ajaxSubmit(options);
            return false;
        });
    });


    function showRequest(){
        var val_arr=[];
        var name_arr=[];
        var flag=false;
        $(".form-control").each(function(){
            if($(this).val()!==''){
                val_arr.push($(this).val());
            }else{
                val_arr.push(' ');
            }

        });
        $('.col-sm-2').each(function(){
            name_arr.push($(this).html());
        })

        $.each(val_arr,function(i,item){
            if(item==' '){
                flag=true;
                msg_url(name_arr[i]+'不能为空');
                return false;
            }
        });
        if(flag){
            return false;
        }

    }

    function showResponse1(responseText, statusText, xhr, form){
        if(statusText=='success'){
            layer.closeAll('loading');
            msg_url(responseText.info,responseText.url);
        }
    }
</script>

需要加载ajaxform的插件

猜你喜欢

转载自my.oschina.net/u/3660147/blog/1788752
今日推荐