使用Javascript或jQuery在HTML表中自动增加数据

时间:2012-11-17 17:28:59

标签: javascript jquery html html-table

我是javascript的新手。我在HTML表中遇到问题。 (这只是一个参考,因为整个表格的范围是100,000到2,000,000。)

这是一个列表,它有点大,如果我输入一个“范围”错误,手动创建这个将需要很长时间,我将不得不编辑并重新安排,然后检查“范围”是否正确再次。我希望这可以在javascript中完成。任何人都可以帮我把“范围”变成“r-1和r-2”之类的变量,一个函数可以自动将变量增量填充到5,000或10,000,从100,000到2,000,000。

HTML表格

jsFiddle

<table align="center" width="100%" border="0" cellspacing="0" cellpadding="0">
     <tr>
          <td class="range" width="15%">100,000 - 105,000</td><td class="value" width="10%">VALUE</td>

          <td class="range" width="15%">125,000 - 130,000</td><td class="value" width="10%">VALUE</td>

          <td class="range" width="15%">150,000 - 155,000</td><td class="value" width="10%">VALUE</td>

          <td class="range" width="15%">175,000 - 180,000</td><td class="value" width="10%">VALUE</td>
     </tr>

     <tr>
          <td class="range" width="15%">105,000 - 110,000</td><td class="value" width="10%">VALUE</td>

          <td class="range" width="15%">130,000 - 135,000</td><td class="value" width="10%">VALUE</td>

          <td class="range" width="15%">155,000 - 160,000</td><td class="value" width="10%">VALUE</td>

          <td class="range" width="15%">180,000 - 185,000</td><td class="value" width="10%">VALUE</td>
     </tr>

     <tr>
          <td class="range" width="15%">110,000 - 115,000</td><td class="value" width="10%">VALUE</td>

          <td class="range" width="15%">135,000 - 140,000</td><td class="value" width="10%">VALUE</td>

          <td class="range" width="15%">160,000 - 165,000</td><td class="value" width="10%">VALUE</td>

          <td class="range" width="15%">185,000 - 190,000</td><td class="value" width="10%">VALUE</td>
     </tr>

     <tr>
          <td class="range" width="15%">115,000 - 120,000</td><td class="value" width="10%">VALUE</td>

          <td class="range" width="15%">140,000 - 145,000</td><td class="value" width="10%">VALUE</td>

          <td class="range" width="15%">165,000 - 170,000</td><td class="value" width="10%">VALUE</td>

          <td class="range" width="15%">190,000 - 195,000</td><td class="value" width="10%">VALUE</td>
     </tr>

     <tr>
          <td class="range" width="15%">120,000 - 125,000</td><td class="value" width="10%">VALUE</td>

          <td class="range" width="15%">145,000 - 150,000</td><td class="value" width="10%">VALUE</td>

          <td class="range" width="15%">170,000 - 175,000</td><td class="value" width="10%">VALUE</td>

          <td class="range" width="15%">195,000 - 200,000</td><td class="value" width="10%">VALUE</td>
     </tr>
</table>​

2 个答案:

答案 0 :(得分:2)

这将构建显示的表结构

 $(function(){ 
     /* start & end divided by 1000*/
    var start=100, end=2000, increment=25, rowCellCount=4;;
    var html=[];
    for(i=start; i<= end- (increment*rowCellCount)+increment ; i=i+5){
        html.push('<tr>');
        for( j=0; j< rowCellCount; j++){
            var valueStart= (i+j*increment), valueEnd=valueStart+5;

            var text= parseThousands(valueStart)+',000 ';
            text+= valueEnd <= end ? ' - ' + parseThousands(valueEnd)+',000' :'';        

            html.push( '<td class="range" width="15%">'+text+'</td><td class="value" width="10%">VALUE</td>');
         }
         html.push('</tr>');
    }

    $('table').html( html.join(''))

 });

    function parseThousands( val){
        var txt =val.toString();
        if( txt.length==4){       
             txt= txt.slice(0,1) +','+ txt.slice(1)       
        }
        return txt;
    }

DEMO:http://jsfiddle.net/Nck5B/20/

答案 1 :(得分:1)

不确定。旧代码变得多余,所以我删除了它。查看jsFiddle:http://jsfiddle.net/WFqTX/4/

您现在可以控制开始,结束,步骤和所有其他参数:

    /* here we define variables to dynamically create your
    data, so you don't have to write it manually */

    var start = 100000;
    var end = 125000;
    var step = 5000;

    /* for additional variability we will set number of
    columns as a variable */

    var colN = 4;