JavaScript验证table任意单元格的值是否为空

 

代码
   
     
1 var objTable = document.getElementById( " tableContent " );
2 var objTR;
3 var objTD;
4 var objInput;
5 for ( var i = 2 ; i < objTable.rows.length ; i ++ )
6 {
7 objTR = objTable.rows[i];
8 for ( var j = 1 ; j < objTR.cells.length; j ++ )
9 {
10 objTD = objTR.cells[j];
11 objInput = objTD.childNodes[ 0 ];
12 if (objInput.value == "" )
13 {
14 alert( ' 请确认表格内数据填写完整! ' );
15 objInput.focus();
16 event.returnValue = false ;
17 return ;
18 }
19 }
20 }

 

代码
   
     
1 var tbobj = document.getElementById( " tableContent " );
2   for ( var i = 0 ;i < tbobj.rows.length;i ++ )
  {
3 var trobj = tbobj.rows[i];
4 for ( var j = 0 ;j < trobj.cells.length;j ++ )
    {
5 var tdobj = trobj.cells[j];
6 if (tdobj.childNodes.length == 0 )
7 {
8 alert( ' EMPTY ' )
9 }
10 else
11 {
12 for ( var k = 0 ;k < tdobj.childNodes.length;k ++ ){
13 var childNode = tdobj.childNodes[k];
14 if (childNode != null )
15         {
16 alert(childNode.value)
17         }
18 }
19    }
20    }
21 }

 

代码
   
     
1 var tbobj = document.getElementById(autoTableId);
2 for ( var i = 0 ;i < tbobj.rows.length;i ++ ){
3 var trobj = tbobj.rows[i];
4 for ( var j = 0 ;j < trobj.cells.length;j ++ ){
5 var tdobj = trobj.cells[j];
6 for ( var k = 0 ;k < tdobj.childNodes.length;k ++ ){
7 var childNode = tdobj.childNodes[k];
8 if ( typeof (childNode.name) != " undefined " ){
9 alert(childNode.name);
10 }
11 }
12 }
13 }
14
15  

 注:我是从第三行第二列开始验证的。有人说JS中判断对象非空,应为if(object)或if(!object);不应该if(object != null),这是错的~ 判断对象未定义为:if( typeof(object)!= "undefined" ){}

你可能感兴趣的:(JavaScript)