s = x+y
rst = 1*(s>=0&&s<10) + 2*(s>=10&&s<100) + 3*(s>=100&&s<1000) + 4*(s>=1000&&s<10000) + 5*(s>=10000&&s<100000) + 6*(s>=100000&&s<1000000)
return rst
}
商品列表页<!--{foreach from=$goods_list item=goods}-->
<!--{if $goods.goods_id} -->
<li>
<a href="{$goods.url}">
<img style="display: inline" src="{$goods.goods_thumb}" alt="{$goods.goods_name}" height="140" width="140">
</a>
<p>
<a href="{$goods.url}">{$goods.goods_name}</a>
</p>
<h4><!-- {if $goods.promote_price neq ""} -->
{$goods.promote_price}
<!-- {else}-->
{$goods.shop_price}
<!--{/if}--></h4>
<font>已有<a class="review_" href="{$goods.url}">{$goods.comment_count}</a>人评价</font>
<h5>
<a href="javascript:addToCart({$goods.goods_id})">立即购买</a>
<a href='javascript:collect({$goods.goods_id})' class="shouc_btn">收藏</a>
</h5>
</li>
<!-- {/if} -->
<!--{/foreach}-->
{$goods.goods_id}就是获取商品列表页面的商品ID
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title>循环填充数据</title>
<style type="text/css">
td{
width: 50px
height: 50px
}
</style>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="0">
<tr><th colspan="6">数据表格</th></tr>
<tr><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td></tr>
</table>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
//模拟后台数据,数据量可变
var data = [{
message:"数据1"
},
{
message:"数据2"
},
{
message:"数据3"
},
{
message:"数据4"
},
{
message:"数据5"
},
{
message:"数据6"
},
{
message:"数据7"
},
{
message:"数据8"
},
{
message:"数据9"
},
{
message:"数据10"
}
]
var tdarr = $("td")//获取表格数量
var num1 = Math.floor(tdarr.length / data.length)//取商 表格数量除以后台数据数量
var num2 = tdarr.length % data.length//取余 表格数量除以后台数据数量
//console.log("num1:"+num1)
//console.log("num2:"+num2)
//将能整除的部分循环填充后台数据
for(var i=0i<num1i++){
$.each(data,function(index){
//console.log(index+i*data.length)
//console.log(data[index].message)
$(tdarr[index+i*data.length]).text(data[index].message)
})
}
//如果不能整除即取余不为0,则再循环补填未填充的表格
for(var i=0i<num2i++){
$(tdarr[data.length*num1+i]).text(data[i].message)
}
</script>
</body>
</html>