jquery遍历数组

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");
          }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值