jquery遍历数组

来源:互联网 发布:java打印上下杨辉三角 编辑:程序博客网 时间:2024/05/22 01:42

1.用$(arrays).each(function(index,element){ })的方式
index是可选参数表示是数组中的第几个元素;
element是当前遍历到的元素;

//获取每一列的表头信息

var tabhead = $("#cpggtable .producthead")$(tabhead).each(function(i,n){                   //ok                   var name = $(this).attr("name");                   //alert(name);                   if(name=="isValidity"){                        tableRow.insertCell(i).innerHTML =                             "是<input class='productisValidity' type = 'radio' name="+k+" value='true' checked='checked'/>    否<input class='productisValidity' type = 'radio' name="+k+" value='false'/>";                        k++;                   }else if(name=="descript"){                        tableRow.insertCell(i).innerHTML= "<textarea class='producvalue producw' name='producvalue'></textarea>";                   }else if(name =="productValid"){                        tableRow.insertCell(i).innerHTML= "<input class='producvalue' type = 'text' name='producvalue' onkeyup=this.value=this.value.replace(/[^0-9.]/g,'') onafterpaste=this.value=this.value.replace(/[^0-9.]/g,'')/>";                   }else if(name == "productprice"){                        tableRow.insertCell(i).innerHTML= "<input class='producvalue' type = 'text' name='producvalue'/>";                   }                   else if(name == "productField1"){                        tableRow.insertCell(i).innerHTML="<input class='producvalue' type = 'text' name='productField1' onkeyup=this.value=this.value.replace(/[^0-9.]/g,'') onafterpaste=this.value=this.value.replace(/[^0-9.]/g,'')/>";                   }              })

2.用数组下标的方式进行添加

//获取每一列的表头信息

          var tabhead = $("#cpggtable .producthead");          for(var j = 0;j<tabhead.length;j++){              var name2 = $(tabhead[j]).attr("name");              alert(name2);          }

3.$.each(xxx, function() {})遍历一个解析好的对象

 var productSpecj = eval('$!productSpecJsons');     if(typeof(productSpecj)=="undefined" || productSpecj=="")         return;     var productSpecRows = $("#cpggtable").find('tr[class="productspecrow"]');    var productSpecId = $(op).find("option:selected").attr("id");    $.each(productSpecj, function() {        if(this.id==productSpecId){            var productSecDesc = this.descript;            var productPrice = this.price;            var productValid = this.productValid            $("#cpggtable").find('li[id="productDes"]').html("产品描述:"+productSecDesc);            $("#cpggtable").find('li[id="productValid"]').html("产品期限:"+productValid+"年");            $("#cpggtable").find('span[id="productprice"]').val(productPrice.toFixed(2));            $("#cpggtable").find('span[id="productprice"]').html("¥"+productPrice);        };    });
错误的不可行方式://获取每一列的表头信息          var tabhead = $("#cpggtable .producthead");          for(var j = 0;j<tabhead.length;j++){              //buxing              //$(tabhead).get[j].attr("name");              //buxing              //var input = tabhead.get[j];              //alert(input);              //var name = $(input).attr("name");          }
0 0
原创粉丝点击