function checkInput(obj){
this.obj = obj;
}
checkInput.prototype = {
init:function(){
var oInputs = this.obj.getElementsByTagName('input');
var ob = [];
if(oInputs[0].value == '' && oInputs[1].value == '' || oInputs[0].value != '' && oInputs[1].value != ''){
ob.push(1);
}else{
ob.push(0);
}
return ob;
}
}window.onload = function(){
var lis = document.getElementById('gab').getElementsByTagName('li');
var btn = document.getElementById('btn');
btn.onclick = function(){
var oTest = [];
for(var i=0,len=lis.length; i<len; i++){
var test = new checkInput(lis[i]);
oTest.push(test.init());
}
for (var i=0,len = oTest.length; i<len; i++){
if(oTest[i] == 0){
alert("不能为空");
oTest.length = 0;
return;
}
}
}
}

解决方案 »

  1.   

    function checkInput(obj){
        this.obj = obj;
    }
    checkInput.prototype = {
        init:function(){
            var oInputs = this.obj.getElementsByTagName('input');
            var ob = [];
            if(oInputs[0].value == '' && oInputs[1].value == '' || oInputs[0].value != '' && oInputs[1].value != ''){
                ob.push(1);
            }else{
                ob.push(0);
            }
            return ob;
        }
    }window.onload = function(){
        var lis = document.getElementById('gab').getElementsByTagName('li');
        var btn = document.getElementById('btn');
        btn.onclick = function(){
            var oTest = [];
            for(var i=0,len=lis.length; i<len; i++){
                var test = new checkInput(lis[i]);
                oTest.push(test.init());
            }
            for (var i=0,len = oTest.length; i<len; i++){
                if(oTest[i] == 0){
                    alert("不能为空");
                    oTest.length = 0;
                    return;
                }
            }
        }
    }
      

  2.   


    if((nameObjs[i].value != "" && telObjs[i].value == "")) {
                alert("不能为空。");
                return false;
    }这样的确很简单,也可以直接用class来表示就可以了,每个li里面的所以input不能为空就可以了。jquery这样判断就比较好判断了